1DELETE(7)                PostgreSQL 13.4 Documentation               DELETE(7)
2
3
4

NAME

6       DELETE - delete rows of a table
7

SYNOPSIS

9       [ WITH [ RECURSIVE ] with_query [, ...] ]
10       DELETE FROM [ ONLY ] table_name [ * ] [ [ AS ] alias ]
11           [ USING from_item [, ...] ]
12           [ WHERE condition | WHERE CURRENT OF cursor_name ]
13           [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
14

DESCRIPTION

16       DELETE deletes rows that satisfy the WHERE clause from the specified
17       table. If the WHERE clause is absent, the effect is to delete all rows
18       in the table. The result is a valid, but empty table.
19
20           Tip
21           TRUNCATE(7) provides a faster mechanism to remove all rows from a
22           table.
23
24       There are two ways to delete rows in a table using information
25       contained in other tables in the database: using sub-selects, or
26       specifying additional tables in the USING clause. Which technique is
27       more appropriate depends on the specific circumstances.
28
29       The optional RETURNING clause causes DELETE to compute and return
30       value(s) based on each row actually deleted. Any expression using the
31       table's columns, and/or columns of other tables mentioned in USING, can
32       be computed. The syntax of the RETURNING list is identical to that of
33       the output list of SELECT.
34
35       You must have the DELETE privilege on the table to delete from it, as
36       well as the SELECT privilege for any table in the USING clause or whose
37       values are read in the condition.
38

PARAMETERS

40       with_query
41           The WITH clause allows you to specify one or more subqueries that
42           can be referenced by name in the DELETE query. See Section 7.8 and
43           SELECT(7) for details.
44
45       table_name
46           The name (optionally schema-qualified) of the table to delete rows
47           from. If ONLY is specified before the table name, matching rows are
48           deleted from the named table only. If ONLY is not specified,
49           matching rows are also deleted from any tables inheriting from the
50           named table. Optionally, * can be specified after the table name to
51           explicitly indicate that descendant tables are included.
52
53       alias
54           A substitute name for the target table. When an alias is provided,
55           it completely hides the actual name of the table. For example,
56           given DELETE FROM foo AS f, the remainder of the DELETE statement
57           must refer to this table as f not foo.
58
59       from_item
60           A table expression allowing columns from other tables to appear in
61           the WHERE condition. This uses the same syntax as the FROM clause
62           of a SELECT statement; for example, an alias for the table name can
63           be specified. Do not repeat the target table as a from_item unless
64           you wish to set up a self-join (in which case it must appear with
65           an alias in the from_item).
66
67       condition
68           An expression that returns a value of type boolean. Only rows for
69           which this expression returns true will be deleted.
70
71       cursor_name
72           The name of the cursor to use in a WHERE CURRENT OF condition. The
73           row to be deleted is the one most recently fetched from this
74           cursor. The cursor must be a non-grouping query on the DELETE's
75           target table. Note that WHERE CURRENT OF cannot be specified
76           together with a Boolean condition. See DECLARE(7) for more
77           information about using cursors with WHERE CURRENT OF.
78
79       output_expression
80           An expression to be computed and returned by the DELETE command
81           after each row is deleted. The expression can use any column names
82           of the table named by table_name or table(s) listed in USING. Write
83           * to return all columns.
84
85       output_name
86           A name to use for a returned column.
87

OUTPUTS

89       On successful completion, a DELETE command returns a command tag of the
90       form
91
92           DELETE count
93
94       The count is the number of rows deleted. Note that the number may be
95       less than the number of rows that matched the condition when deletes
96       were suppressed by a BEFORE DELETE trigger. If count is 0, no rows were
97       deleted by the query (this is not considered an error).
98
99       If the DELETE command contains a RETURNING clause, the result will be
100       similar to that of a SELECT statement containing the columns and values
101       defined in the RETURNING list, computed over the row(s) deleted by the
102       command.
103

NOTES

105       PostgreSQL lets you reference columns of other tables in the WHERE
106       condition by specifying the other tables in the USING clause. For
107       example, to delete all films produced by a given producer, one can do:
108
109           DELETE FROM films USING producers
110             WHERE producer_id = producers.id AND producers.name = 'foo';
111
112       What is essentially happening here is a join between films and
113       producers, with all successfully joined films rows being marked for
114       deletion. This syntax is not standard. A more standard way to do it is:
115
116           DELETE FROM films
117             WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');
118
119       In some cases the join style is easier to write or faster to execute
120       than the sub-select style.
121

EXAMPLES

123       Delete all films but musicals:
124
125           DELETE FROM films WHERE kind <> 'Musical';
126
127       Clear the table films:
128
129           DELETE FROM films;
130
131       Delete completed tasks, returning full details of the deleted rows:
132
133           DELETE FROM tasks WHERE status = 'DONE' RETURNING *;
134
135       Delete the row of tasks on which the cursor c_tasks is currently
136       positioned:
137
138           DELETE FROM tasks WHERE CURRENT OF c_tasks;
139

COMPATIBILITY

141       This command conforms to the SQL standard, except that the USING and
142       RETURNING clauses are PostgreSQL extensions, as is the ability to use
143       WITH with DELETE.
144

SEE ALSO

146       TRUNCATE(7)
147
148
149
150PostgreSQL 13.4                      2021                            DELETE(7)
Impressum