DELETE(7)
π DELETE(7) β the "yeet those rows" page π
okay bestie, buckle up, because we're talking about DELETE from the PostgreSQL 16.15 Documentation β yes, THE delete. the one you've been thinking about at 2am. let's get into it β¨
πΈ NAME πΈ
DELETE β delete rows of a table. that's it. that's the whole vibe. rows go poof π¨
β¨ SYNOPSIS β¨
[ WITH [ RECURSIVE ] with_query [, ...] ]
DELETE FROM [ ONLY ] table_name [ * ] [ [ AS ] alias ]
[ USING from_item [, ...] ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING { * | output_expression [ [ AS ] output_name ] } [, ...] ]
literally every bracket here is optional except the DELETE FROM table_name part, no cap π«Ά
π DESCRIPTION π
okay so DELETE deletes rows that satisfy the WHERE clause from the specified table. that's the whole point. and if the WHERE clause is absent?? it deletes all the rows in the table. the result is a valid, but empty table. like, it's still a table, it's justβ¦ going through something π
π Tip: TRUNCATE provides a faster mechanism to remove all rows from a table. so if you're nuking everything, maybe go that route instead, iykyk.
there are two ways to delete rows in a table using information contained in other tables in the database:
- using sub-selects
- specifying additional tables in the
USINGclause
which technique is more appropriate depends on the specific circumstances β it's giving "depends on the situation" energy, and that's fr fr the answer πββοΈ
the optional RETURNING clause causes DELETE to compute and return value(s) based on each row actually deleted. any expression using the table's columns, and/or columns of other tables mentioned in USING, can be computed. the syntax of the RETURNING list is identical to that of the output list of SELECT. iconic behavior honestly π
and permissions?? you must have the DELETE privilege on the table to delete from it, as well as the SELECT privilege for any table in the USING clause or whose values are read in the condition. no privilege, no deletion. the vibes check you π
π PARAMETERS π
with_queryβ theWITHclause allows you to specify one or more subqueries that can be referenced by name in theDELETEquery. see Section 7.8 andSELECT(7)for details. very "pre-game" of you β¨table_nameβ the name (optionally schema-qualified) of the table to delete rows from. ifONLYis specified before the table name, matching rows are deleted from the named table only. ifONLYis not specified, matching rows are also deleted from any tables inheriting from the named table. optionally,*can be specified after the table name to explicitly indicate that descendant tables are included. soONLY= just us, noONLY= bring the whole friend group π«Άaliasβ a substitute name for the target table. when an alias is provided, it completely hides the actual name of the table. for example, givenDELETE FROM foo AS f, the remainder of theDELETEstatement must refer to this table asfnotfoo. like a stage name, bestie.foois not coming to the phone π΅from_itemβ a table expression allowing columns from other tables to appear in theWHEREcondition. this uses the same syntax as theFROMclause of aSELECTstatement; for example, an alias for the table name can be specified. do not repeat the target table as afrom_itemunless you wish to set up a self-join (in which case it must appear with an alias in thefrom_item). self-join said "i'm main character energy" πconditionβ an expression that returns a value of typeboolean. only rows for which this expression returns true will be deleted. true = gone. false/null = you live another day πcursor_nameβ the name of the cursor to use in aWHERE CURRENT OFcondition. the row to be deleted is the one most recently fetched from this cursor. the cursor must be a non-grouping query on theDELETE's target table. note thatWHERE CURRENT OFcannot be specified together with a Boolean condition. seeDECLARE(7)for more information about using cursors withWHERE CURRENT OF. it's giving "pick one lane" π£οΈoutput_expressionβ an expression to be computed and returned by theDELETEcommand after each row is deleted. the expression can use any column names of the table named bytable_nameor table(s) listed inUSING. write*to return all columns. all of them. the whole fit πoutput_nameβ a name to use for a returned column. name your children, bestie π
π₯ OUTPUTS π₯
on successful completion, a DELETE command returns a command tag of the form
DELETE count
the count is the number of rows deleted. note that the number may be less than the number of rows that matched the condition when deletes were suppressed by a BEFORE DELETE trigger. like, the trigger said "not today" π
if count is 0, no rows were deleted by the query (this is not considered an error). zero deleted, zero problems, we move πΈ
if the DELETE command contains a RETURNING clause, the result will be similar to that of a SELECT statement containing the columns and values defined in the RETURNING list, computed over the row(s) deleted by the command. it's giving "receipts for the deleted" π§Ύβ¨
π¦ NOTES π¦
PostgreSQL lets you reference columns of other tables in the WHERE condition by specifying the other tables in the USING clause. for example, to delete all films produced by a given producer, one can do:
DELETE FROM films USING producers
WHERE producer_id = producers.id AND producers.name = 'foo';
so what is essentially happening here is a join between films and producers, with all successfully joined films rows being marked for deletion. this syntax is not standard. β οΈ
a more standard way to do it is:
DELETE FROM films
WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');
in some cases the join style is easier to write or faster to execute than the sub-select style. so like, both are valid, one's just built different depending on the day πββοΈ
β EXAMPLES β
delete all films but musicals (musicals stay, they ate):
DELETE FROM films WHERE kind <> 'Musical';
clear the table films (empty. void. cleared. π«§):
DELETE FROM films;
delete completed tasks, returning full details of the deleted rows (receipts!! π):
DELETE FROM tasks WHERE status = 'DONE' RETURNING *;
delete the row of tasks on which the cursor c_tasks is currently positioned (cursor said "this one" π―):
DELETE FROM tasks WHERE CURRENT OF c_tasks;
π COMPATIBILITY π
this command conforms to the SQL standard, except that the USING and RETURNING clauses are PostgreSQL extensions, as is the ability to use WITH with DELETE. so we're standard and we're extra. periodt π
π SEE ALSO π
TRUNCATE(7) β your other bestie for the "remove everything" moments π
and that's the page, bestie!! brought to you by PostgreSQL 16.15, dated 2026 β go forth and delete responsibly, mwah π§π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms