DELETE(7)

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

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

  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 "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 β€” 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. it's giving pre-game planning. πŸ“
  • 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. 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, given DELETE FROM foo AS f, the remainder of the DELETE statement must refer to this table as f not foo. so like, deadname rules. don't use foo after that, it's rude and also an error. πŸ™ˆ
  • 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). read that again bestie, it's important. ⚠️
  • condition β€” an expression that returns a value of type boolean. only rows for which this expression returns true will be deleted. truthy = gone. 🫑
  • 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 "this one specific row, not the whole squad." 🎯
  • 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. 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