DELETE(7)
DELETE(7) π β¨ the manual but make it group chat β¨π
okay so bestie, welcome to DELETE(7) β the PostgreSQL 15.19 documentation but with main character energy. this page is literally about deleting rows of a table. no cap, it's giving "spring cleaning but for your database". let's get into it π«Ά
π NAME π
DELETE β delete rows of a table
π 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 ] } [, ...] ]
β¨ DESCRIPTION β¨
okay so DELETE deletes rows that satisfy the WHERE clause from the specified table. if the WHERE clause is absent, the effect is to delete all rows in the table. the result is a valid, but empty table. literally icon behavior β it ate and left no crumbs π½οΈ
π‘ Tip: TRUNCATE provides a faster mechanism to remove all rows from a table. iykyk β sometimes you gotta use the speedrun strat, but that's a whole other manpage bestie.
there are two ways to delete rows in a table using information contained in other tables in the database: using sub-selects, or specifying additional tables in the USING clause. which technique is more appropriate depends on the specific circumstances. it's giving "choose your fighter" energy fr fr π
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. slay π
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 bestie β no gatekeeping allowed π
ββοΈ
πΈ 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 "subquery era" β¨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. the way thatONLYchanges everythingβ¦ periodt π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. not me gatekeeping the table's real identity π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). lowkey important β don't repeat yourself bestie π«₯conditionβ an expression that returns a value of typeboolean. only rows for which this expression returnstruewill be deleted. vibe check: if it ain'ttrue, it stays. facts π―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. cursor drama is real, but it's giving precision π―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. main character energy: give the people what they want πoutput_nameβ a name to use for a returned column. name it and claim it 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. if count is 0, no rows were deleted by the query (this is not considered an error). zero is valid bestie, don't panic π
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" π§Ύ
π¦ 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. 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. choose your vibe, bestie π«
π₯ 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. we're special like that π
β¨
π SEE ALSO π
TRUNCATE(7) β the other way to clear the table, no cap
and that's the tea bestie π΅ this page brought to you by PostgreSQL 15.19, dated 2026 β she's iconic, she's valid, she's DELETE(7) and she ate πβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-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