DELETE(7)
π DELETE(7) β the "yeet that row" page, bestie
okay so real talk: this is the DELETE page from the PostgreSQL 17.11 Documentation, and it's genuinely one of the most satisfying commands in the whole manual. it removes rows. that's it. that's the vibe. ποΈβ¨
πΈ NAME πΈ
DELETE β delete rows of a table
like, literally. rows go bye. π
β¨ 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 ] } [, ...] ]
look at that shape. every bracket is optional, but the drama? mandatory. π
π¦ DESCRIPTION π¦
so DELETE deletes the rows that satisfy the WHERE clause, from the table you named. and if there's no WHERE clause at all? it deletes every single row in the table. the result is a valid table that is also completely empty. it's giving "i cleaned my room by throwing everything away." π«Ά
π‘ Tip: TRUNCATE is the faster mechanism for nuking all the rows from a table. same energy, zoomier.
now, there are two ways to delete rows using info that lives in other tables in the database β bestie, take notes:
- using sub-selects
- specifying additional tables in the
USINGclause
which technique is more appropriate? depends on the specific circumstances, bestie. there's no universal answer, no cap.
the optional RETURNING clause makes DELETE compute and return value(s) based on each row that actually got deleted. and you can use any expression over the table's columns and/or the columns of other tables mentioned in USING. the syntax of the RETURNING list is identical to the output list of SELECT. ikyk. β
also, permissions check π: you must have the DELETE privilege on the table to delete from it, and the SELECT privilege for any table in the USING clause, or any table whose values are read in the condition. no privilege, no party.
π PARAMETERS π
with_queryβ theWITHclause lets you specify one or more subqueries that can be referenced by name in theDELETEquery. see Section 7.8 andSELECT(7)for the details, i'm not retyping the whole lore πtable_nameβ the name (optionally schema-qualified) of the table to delete rows from. ifONLYis specified before the table name, matching rows get deleted from the named table only. ifONLYis not specified, matching rows also get deleted from any tables inheriting from the named table. optionally,*can be specified after the table name to explicitly say "yes bestie, descendant tables are included." πaliasβ a substitute name for the target table. and when an alias is provided, it completely hides the actual name of the table. like a full rebrand. for example, givenDELETE FROM foo AS f, the remainder of theDELETEstatement must refer to this table asf, notfoo. no deadnaming. π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). don't do it. i'm serious. πconditionβ an expression that returns a value of typeboolean. only rows for which this expression returns true will be deleted. true = gone. false or null = you live to see 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 β it's one or the other, pick a lane. seeDECLARE(7)for more on using cursors withWHERE CURRENT OF. π§output_expressionβ an expression to be computed and returned by theDELETEcommand after each row is deleted. it can use any column names of the table named bytable_name, or table(s) listed inUSING. write*to return all columns. all of them. the whole wardrobe. πoutput_nameβ a name to use for a returned column. yeah. that's the whole description. slay.
π₯ OUTPUTS π₯
on successful completion, a DELETE command returns a command tag of the form
DELETE count
count is the number of rows deleted. π
and here's the important little detail: that number may be less than the number of rows that matched the condition, when deletes were suppressed by a BEFORE DELETE trigger. triggers said "not today." if count is 0, no rows were deleted by the query β and heads up, this is not considered an error. zero is a valid number of rows to delete. we respect it.
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. so you get receipts. love that for you. π§Ύβ¨
π NOTES π
PostgreSQL lets you reference columns of other tables in the WHERE condition by specifying those 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. it's a PostgreSQL thing. it's iconic, but it's 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. depends on the circumstances, as established. π¦
β EXAMPLES β
delete all films but musicals β because musicals are safe, they're the main characters:
DELETE FROM films WHERE kind <> 'Musical';
clear the table films β full reset, no thoughts, head empty:
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;
and now the big one π β while there is no LIMIT clause for DELETE, it is possible to get a similar effect using the same method described in the documentation of UPDATE:
WITH delete_batch AS (
SELECT l.ctid FROM user_logs AS l
WHERE l.status = 'archived'
ORDER BY l.creation_date
FOR UPDATE
LIMIT 10000
)
DELETE FROM user_logs AS dl
USING delete_batch AS del
WHERE dl.ctid = del.ctid;
π this use ofctidis only safe because the query is repeatedly run, avoiding the problem of changedctids. do not run this once and then walk away thinking you're a genius. you're not. i say this with love.
π 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 extra sparkle. β¨
π«Ά SEE ALSO π«Ά
and that's the whole page, bestie. no notes. π
signing off with love from the PostgreSQL 17.11 docs, dated 2026 β DELETE(7) and out. ππ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-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