1DELETE(7) PostgreSQL 9.2.24 Documentation DELETE(7)
2
3
4
6 DELETE - delete rows of a table
7
9 [ WITH [ RECURSIVE ] with_query [, ...] ]
10 DELETE FROM [ ONLY ] table_name [ * ] [ [ AS ] alias ]
11 [ USING using_list ]
12 [ WHERE condition | WHERE CURRENT OF cursor_name ]
13 [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
14
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) is a PostgreSQL extension that provides a faster
22 mechanism to remove all rows from a 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
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,
43 “WITH Queries (Common Table Expressions)”, in the documentation and
44 SELECT(7) for details.
45
46 table_name
47 The name (optionally schema-qualified) of the table to delete rows
48 from. If ONLY is specified before the table name, matching rows are
49 deleted from the named table only. If ONLY is not specified,
50 matching rows are also deleted from any tables inheriting from the
51 named table. Optionally, * can be specified after the table name to
52 explicitly indicate that descendant tables are included.
53
54 alias
55 A substitute name for the target table. When an alias is provided,
56 it completely hides the actual name of the table. For example,
57 given DELETE FROM foo AS f, the remainder of the DELETE statement
58 must refer to this table as f not foo.
59
60 using_list
61 A list of table expressions, allowing columns from other tables to
62 appear in the WHERE condition. This is similar to the list of
63 tables that can be specified in the FROM Clause of a SELECT
64 statement; for example, an alias for the table name can be
65 specified. Do not repeat the target table in the using_list, unless
66 you wish to set up a self-join.
67
68 condition
69 An expression that returns a value of type boolean. Only rows for
70 which this expression returns true will be deleted.
71
72 cursor_name
73 The name of the cursor to use in a WHERE CURRENT OF condition. The
74 row to be deleted is the one most recently fetched from this
75 cursor. The cursor must be a non-grouping query on the DELETE's
76 target table. Note that WHERE CURRENT OF cannot be specified
77 together with a Boolean condition. See DECLARE(7) for more
78 information about using cursors with WHERE CURRENT OF.
79
80 output_expression
81 An expression to be computed and returned by the DELETE command
82 after each row is deleted. The expression can use any column names
83 of the table named by table_name or table(s) listed in USING. Write
84 * to return all columns.
85
86 output_name
87 A name to use for a returned column.
88
90 On successful completion, a DELETE command returns a command tag of the
91 form
92
93 DELETE count
94
95 The count is the number of rows deleted. Note that the number may be
96 less than the number of rows that matched the condition when deletes
97 were suppressed by a BEFORE DELETE trigger. If count is 0, no rows were
98 deleted by the query (this is not considered an error).
99
100 If the DELETE command contains a RETURNING clause, the result will be
101 similar to that of a SELECT statement containing the columns and values
102 defined in the RETURNING list, computed over the row(s) deleted by the
103 command.
104
106 PostgreSQL lets you reference columns of other tables in the WHERE
107 condition by specifying the other tables in the USING clause. For
108 example, to delete all films produced by a given producer, one can do:
109
110 DELETE FROM films USING producers
111 WHERE producer_id = producers.id AND producers.name = 'foo';
112
113 What is essentially happening here is a join between films and
114 producers, with all successfully joined films rows being marked for
115 deletion. This syntax is not standard. A more standard way to do it is:
116
117 DELETE FROM films
118 WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');
119
120 In some cases the join style is easier to write or faster to execute
121 than the sub-select style.
122
124 Delete all films but musicals:
125
126 DELETE FROM films WHERE kind <> 'Musical';
127
128 Clear the table films:
129
130 DELETE FROM films;
131
132 Delete completed tasks, returning full details of the deleted rows:
133
134 DELETE FROM tasks WHERE status = 'DONE' RETURNING *;
135
136 Delete the row of tasks on which the cursor c_tasks is currently
137 positioned:
138
139 DELETE FROM tasks WHERE CURRENT OF c_tasks;
140
142 This command conforms to the SQL standard, except that the USING and
143 RETURNING clauses are PostgreSQL extensions, as is the ability to use
144 WITH with DELETE.
145
146
147
148PostgreSQL 9.2.24 2017-11-06 DELETE(7)