DELETE(7)
π DELETE(7) π
okay so bestie, buckle up β this is the DELETE page from the PostgreSQL 14.24 Documentation, and it's literally the glow-up your table needs when it's time to let things go. β¨
πΈ NAME πΈ
DELETE β delete rows of a table. that's it. that's the vibe. ποΈ
π 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 ] } [, ...] ]
like, yes it looks like a lot but every bracket is there for a reason, i promise. π«Ά
π¦ DESCRIPTION π¦
so DELETE deletes rows that satisfy the WHERE clause from the table you specified. and if you don't give it a WHERE clause? π it deletes all the rows in the table. the result is a valid, but empty table. no cap β valid, just empty. like your group chat after everyone went to sleep.
π‘ Tip TRUNCATE provides a faster mechanism to remove all rows from a table. not me telling you there's a speedrun route π
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 "situationship, not a rule." π
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.
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. permissions check first, always. π
β¨ 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. it's giving pre-game planning. π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. yes the star means "and the children too" β main character energy extends to the whole family tree. π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. so like, deadname rules. don't usefooafter that, it's rude and also an error. π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). read that again bestie, it's important. β οΈconditionβ an expression that returns a value of typeboolean. only rows for which this expression returns true will be deleted. truthy = gone. π«‘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 "this one specific row, not the whole squad." π―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. receipts, basically. π§Ύoutput_nameβ a name to use for a returned column. name your output, it deserves it. π
π 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. so if the math isn't mathing, check your triggers. π
if count is 0, no rows were deleted by the query β and this is not considered an error. zero is a valid number bestie, it's fine, it's fine. π
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 "you deleted it AND you get to keep the memory." π¦
π 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';
what is essentially happening here is a join between films and producers, with all successfully joined films rows being marked for deletion. so it's like, a group project where only the matching members get dropped. π
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. pick your fighter, both are valid. π
π₯ EXAMPLES π₯
delete all films but musicals:
DELETE FROM films WHERE kind <> 'Musical';
clear the table films:
DELETE FROM films;
delete completed tasks, returning full details of the deleted rows:
DELETE FROM tasks WHERE status = 'DONE' RETURNING *;
delete the row of tasks on which the cursor c_tasks is currently positioned:
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 like, mostly standard with a few extra sparkles that are ours. π«
π SEE ALSO π
TRUNCATE(7)β the other way to clear everything, and yes, it's faster, we mentioned it. πββοΈ
okay that's the whole page, you ate that, no crumbs. π this masterpiece is from PostgreSQL 14.24, dated 2026, and it lives at page DELETE(7) β go forth and delete responsibly, bestie. πβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-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