DELETE(7)

๐Ÿ“ฆ version 18.6, in opensuse-leap 16.0

๐Ÿ’… 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 โ€” the WITH clause lets you specify one or more subqueries that can be referenced by name in the DELETE query. go look at Section 7.8 and SELECT(7) for the details, iykyk.
  • table_name โ€” the name (optionally schema-qualified) of the table you're deleting rows from. if ONLY is specified before the table name, matching rows get deleted from the named table only. if ONLY isn'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, given DELETE FROM foo AS f, the rest of the DELETE statement has to refer to this table as f, not foo. no takebacks.
  • from_item โ€” a table expression that lets columns from other tables show up in the WHERE condition. it uses the same syntax as the FROM clause of a SELECT statement; for example, you can specify an alias for the table name. do not repeat the target table as a from_item unless you actually want to set up a self-join (and in that case it must appear with an alias in the from_item). don't do it. unless you do. ๐Ÿ™ˆ
  • condition โ€” an expression that returns a value of type boolean. 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 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 on using cursors with WHERE CURRENT OF.
  • output_alias โ€” an optional substitute name for OLD or NEW rows in the RETURNING list.

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 the DELETE command after each row is deleted. the expression can use any column names of the table named by table_name or the table(s) listed in USING. 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 of ctid is only safe because the query is repeatedly run, avoiding the problem of changed ctids. 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