3 PostgreSQL documentation
6 <refentry id=
"SQL-DELETE">
8 <refentrytitle id=
"SQL-DELETE-TITLE">DELETE
</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
14 <refname>DELETE
</refname>
15 <refpurpose>delete rows of a table
</refpurpose>
18 <indexterm zone=
"sql-delete">
19 <primary>DELETE
</primary>
24 DELETE FROM [ ONLY ]
<replaceable class=
"PARAMETER">table
</replaceable> [ [ AS ]
<replaceable class=
"parameter">alias
</replaceable> ]
25 [ USING
<replaceable class=
"PARAMETER">usinglist
</replaceable> ]
26 [ WHERE
<replaceable class=
"PARAMETER">condition
</replaceable> | WHERE CURRENT OF
<replaceable class=
"PARAMETER">cursor_name
</replaceable> ]
27 [ RETURNING * |
<replaceable class=
"parameter">output_expression
</replaceable> [ [ AS ]
<replaceable class=
"parameter">output_name
</replaceable> ] [, ...] ]
32 <title>Description
</title>
35 <command>DELETE
</command> deletes rows that satisfy the
36 <literal>WHERE
</literal> clause from the specified table. If the
37 <literal>WHERE
</literal> clause is absent, the effect is to delete
38 all rows in the table. The result is a valid, but empty table.
43 <xref linkend=
"sql-truncate" endterm=
"sql-truncate-title"> is a
44 <productname>PostgreSQL
</productname> extension that provides a
45 faster mechanism to remove all rows from a table.
50 By default,
<command>DELETE
</command> will delete rows in the
51 specified table and all its child tables. If you wish to delete only
52 from the specific table mentioned, you must use the
53 <literal>ONLY
</literal> clause.
57 There are two ways to delete rows in a table using information
58 contained in other tables in the database: using sub-selects, or
59 specifying additional tables in the
<literal>USING
</literal> clause.
60 Which technique is more appropriate depends on the specific
65 The optional
<literal>RETURNING<
/> clause causes
<command>DELETE<
/>
66 to compute and return value(s) based on each row actually deleted.
67 Any expression using the table's columns, and/or columns of other
68 tables mentioned in
<literal>USING
</literal>, can be computed.
69 The syntax of the
<literal>RETURNING<
/> list is identical to that of the
70 output list of
<command>SELECT<
/>.
74 You must have the
<literal>DELETE
</literal> privilege on the table
75 to delete from it, as well as the
<literal>SELECT
</literal>
76 privilege for any table in the
<literal>USING
</literal> clause or
77 whose values are read in the
<replaceable
78 class=
"parameter">condition
</replaceable>.
83 <title>Parameters
</title>
87 <term><literal>ONLY<
/></term>
90 If specified, delete rows from the named table only. When not
91 specified, any tables inheriting from the named table are also processed.
97 <term><replaceable class=
"parameter">table
</replaceable></term>
100 The name (optionally schema-qualified) of an existing table.
106 <term><replaceable class=
"parameter">alias
</replaceable></term>
109 A substitute name for the target table. When an alias is
110 provided, it completely hides the actual name of the table. For
111 example, given
<literal>DELETE FROM foo AS f<
/>, the remainder
112 of the
<command>DELETE
</command> statement must refer to this
113 table as
<literal>f<
/> not
<literal>foo<
/>.
119 <term><replaceable class=
"PARAMETER">usinglist
</replaceable></term>
122 A list of table expressions, allowing columns from other tables
123 to appear in the
<literal>WHERE<
/> condition. This is similar
124 to the list of tables that can be specified in the
<xref
125 linkend=
"sql-from" endterm=
"sql-from-title"> of a
126 <command>SELECT
</command> statement; for example, an alias for
127 the table name can be specified. Do not repeat the target table
128 in the
<replaceable class=
"PARAMETER">usinglist
</replaceable>,
129 unless you wish to set up a self-join.
135 <term><replaceable class=
"parameter">condition
</replaceable></term>
138 An expression that returns a value of type
<type>boolean
</type>.
139 Only rows for which this expression returns
<literal>true<
/>
146 <term><replaceable class=
"PARAMETER">cursor_name
</replaceable></term>
149 The name of the cursor to use in a
<literal>WHERE CURRENT OF<
/>
150 condition. The row to be deleted is the one most recently fetched
151 from this cursor. The cursor must be a non-grouping
152 query on the
<command>DELETE<
/>'s target table.
153 Note that
<literal>WHERE CURRENT OF<
/> cannot be
154 specified together with a Boolean condition. See
155 <xref linkend=
"sql-declare" endterm=
"sql-declare-title">
156 for more information about using cursors with
157 <literal>WHERE CURRENT OF<
/>.
163 <term><replaceable class=
"PARAMETER">output_expression
</replaceable></term>
166 An expression to be computed and returned by the
<command>DELETE<
/>
167 command after each row is deleted. The expression can use any
168 column names of the
<replaceable class=
"PARAMETER">table
</replaceable>
169 or table(s) listed in
<literal>USING<
/>.
170 Write
<literal>*<
/> to return all columns.
176 <term><replaceable class=
"PARAMETER">output_name
</replaceable></term>
179 A name to use for a returned column.
187 <title>Outputs
</title>
190 On successful completion, a
<command>DELETE<
/> command returns a command
193 DELETE
<replaceable class=
"parameter">count
</replaceable>
195 The
<replaceable class=
"parameter">count
</replaceable> is the number
196 of rows deleted. If
<replaceable class=
"parameter">count
</replaceable> is
197 0, no rows matched the
<replaceable
198 class=
"parameter">condition
</replaceable> (this is not considered
203 If the
<command>DELETE<
/> command contains a
<literal>RETURNING<
/>
204 clause, the result will be similar to that of a
<command>SELECT<
/>
205 statement containing the columns and values defined in the
206 <literal>RETURNING<
/> list, computed over the row(s) deleted by the
215 <productname>PostgreSQL
</productname> lets you reference columns of
216 other tables in the
<literal>WHERE<
/> condition by specifying the
217 other tables in the
<literal>USING
</literal> clause. For example,
218 to delete all films produced by a given producer, one can do:
220 DELETE FROM films USING producers
221 WHERE producer_id = producers.id AND producers.name = 'foo';
223 What is essentially happening here is a join between
<structname>films<
/>
224 and
<structname>producers<
/>, with all successfully joined
225 <structname>films<
/> rows being marked for deletion.
226 This syntax is not standard. A more standard way to do it is:
229 WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');
231 In some cases the join style is easier to write or faster to
232 execute than the sub-select style.
237 <title>Examples
</title>
240 Delete all films but musicals:
242 DELETE FROM films WHERE kind
<> 'Musical';
247 Clear the table
<literal>films
</literal>:
254 Delete completed tasks, returning full details of the deleted rows:
256 DELETE FROM tasks WHERE status = 'DONE' RETURNING *;
261 Delete the row of
<structname>tasks<
/> on which the cursor
262 <literal>c_tasks<
/> is currently positioned:
264 DELETE FROM tasks WHERE CURRENT OF c_tasks;
270 <title>Compatibility
</title>
273 This command conforms to the
<acronym>SQL
</acronym> standard, except
274 that the
<literal>USING
</literal> and
<literal>RETURNING<
/> clauses
275 are
<productname>PostgreSQL
</productname> extensions.