DELETE(7)

πŸ“¦ version 16.15, in opensuse-leap 16.0

πŸ’… 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:

  1. using sub-selects
  2. specifying additional tables in the USING clause

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 β€” the WITH clause allows you to specify one or more subqueries that can be referenced by name in the DELETE query. see Section 7.8 and SELECT(7) for details. very "pre-game" of you ✨
  • table_name β€” the name (optionally schema-qualified) of the table to delete rows from. if ONLY is specified before the table name, matching rows are deleted from the named table only. if ONLY is 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. so ONLY = just us, no ONLY = 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, given DELETE FROM foo AS f, the remainder of the DELETE statement must refer to this table as f not foo. like a stage name, bestie. foo is not coming to the phone πŸ“΅
  • from_item β€” a table expression allowing columns from other tables to appear in the WHERE condition. this uses the same syntax as the FROM clause of a SELECT statement; for example, an alias for the table name can be specified. do not repeat the target table as a from_item unless you wish to set up a self-join (in which case it must appear with an alias in the from_item). self-join said "i'm main character energy" πŸ’…
  • condition β€” an expression that returns a value of type boolean. 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 a WHERE CURRENT OF condition. the row to be deleted is the one most recently fetched from this cursor. the cursor must be a non-grouping query on the DELETE's target table. note that WHERE CURRENT OF cannot be specified together with a Boolean condition. see DECLARE(7) for more information about using cursors with WHERE CURRENT OF. it's giving "pick one lane" πŸ›£οΈ
  • output_expression β€” an expression to be computed and returned by the DELETE command after each row is deleted. the expression can use any column names of the table named by table_name or table(s) listed in USING. 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