DELETE(7)
๐
DELETE โ bestie edition (from the DELETE(7) manual page)
okay so you want to โจdeleteโจ some rows. this is the page for you. it lives in the PostgreSQL 18.6 docs and honestly? iconic. let's get into it.
๐ธ NAME
DELETE โ delete rows of a table. that's it. that's the whole vibe. bye bye rows ๐ซถ
โจ SYNOPSIS โจ
[ WITH [ RECURSIVE ] with_query [, ...] ]
DELETE FROM [ ONLY ] table_name [ * ] [ [ AS ] alias ]
[ USING from_item [, ...] ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING [ WITH ( { OLD | NEW } AS output_alias [, ...] ) ]
{ * | output_expression [ [ AS ] output_name ] } [, ...] ]
look at that. beautiful. no notes. ๐
๐ DESCRIPTION
DELETE deletes the rows that satisfy the WHERE clause from the table you named. and if there's no WHERE clause at all? bestie. it deletes every single row in the table. what you're left with is a valid table that's just... empty. a shell. a ghost of its former self ๐ญ
๐ Tip: TRUNCATE is the faster mechanism to remove all rows from a table. if you're nuking the whole thing, use that instead, fr.
so there's two ways to delete rows using info from other tables in your database: using sub-selects, or listing extra tables in the USING clause. which one is more appropriate depends on the specific circumstances โ it's giving "depends on the situation", no cap.
the optional RETURNING clause makes DELETE compute and return value(s) based on each row it actually deleted. you can compute any expression using the table's columns, and/or columns of other tables mentioned in USING. the syntax of the RETURNING list is identical to the output list of SELECT. periodt.
and the permissions check: you must have the DELETE privilege on the table to delete from it, plus the SELECT privilege for any table in the USING clause or any table whose values are read in the condition. no privilege, no deleting. sorry bestie ๐ซ
๐ PARAMETERS ๐
with_queryโ theWITHclause lets you specify one or more subqueries that can be referenced by name in theDELETEquery. go look at Section 7.8 andSELECT(7)for the details, iykyk.table_nameโ the name (optionally schema-qualified) of the table you're deleting rows from. ifONLYis specified before the table name, matching rows get deleted from the named table only. ifONLYisn't specified, matching rows also get deleted from any tables inheriting from the named table. optionally,*can go after the table name to explicitly say "yes include the descendant tables". ๐ซaliasโ a substitute name for the target table. when you give an alias, it completely hides the actual name of the table. for example, givenDELETE FROM foo AS f, the rest of theDELETEstatement has to refer to this table asf, notfoo. no takebacks.from_itemโ a table expression that lets columns from other tables show up in theWHEREcondition. it uses the same syntax as theFROMclause of aSELECTstatement; for example, you can specify an alias for the table name. do not repeat the target table as afrom_itemunless you actually want to set up a self-join (and in that case it must appear with an alias in thefrom_item). don't do it. unless you do. ๐conditionโ an expression that returns a value of typeboolean. only rows where this expression returns true will be deleted. true = gone. false = surviving. main character selection energy.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 on using cursors withWHERE CURRENT OF.output_aliasโ an optional substitute name forOLDorNEWrows in theRETURNINGlist.
by default, old values from the target table can be returned by writing OLD.column_name or OLD.*, and new values can be returned by writing NEW.column_name or NEW.*. when you provide an alias, those names get hidden and you must refer to the old or new rows using the alias. for example: RETURNING WITH (OLD AS o, NEW AS n) o.*, n.*.
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 the table(s) listed inUSING. write*to return all columns.
a column name or * may be qualified using OLD or NEW, or the corresponding output_alias for OLD or NEW, to cause old or new values to be returned. an unqualified column name, or *, or a column name or * qualified using the target table name or alias, will return old values.
for a simple DELETE, all new values will be NULL. however, if an ON DELETE rule causes an INSERT or UPDATE to be executed instead, the new values may be non-NULL. sneaky little plot twist ๐
output_nameโ a name to use for a returned column. cute, name your outputs 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 โ and that is not considered an error. zero deleted, zero drama, we move on.
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. โจ
๐ 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's 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. 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;
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;
๐ heads up: this use ofctidis only safe because the query is repeatedly run, avoiding the problem of changedctids. do not come for me if you run it once and it goes feral.
๐ 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 mostly well-behaved, just a lil extra โจ
๐ธ SEE ALSO ๐ธ
TRUNCATE(7) โ go read that one next, bestie, it's the fast lane.
okay that's the whole page, you ate it up fr fr. this was PostgreSQL 18.6 docs, dated 2026, straight from the DELETE(7) manual. go delete something responsibly ๐โจ๐
๐ฆ where this page comes from
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms