This page in other versions: 8.4 / 9.0 / 9.1 / 9.2 / 9.3  |  Development versions: devel  |  Unsupported versions: 7.1 / 7.2 / 7.3 / 7.4 / 8.0 / 8.1 / 8.2 / 8.3

DELETE

Name

DELETE -- delete rows of a table

Synopsis

DELETE FROM [ ONLY ] table [ WHERE condition ]

Description

DELETE deletes rows that satisfy the WHERE clause from the specified table. If the WHERE clause is absent, the effect is to delete all rows in the table. The result is a valid, but empty table.

Tip: TRUNCATE is a PostgreSQL extension which provides a faster mechanism to remove all rows from a table.

By default, DELETE will delete rows in the specified table and all its subtables. If you wish to only delete from the specific table mentioned, you must use the ONLY clause.

You must have the DELETE privilege on the table to delete from it, as well as the SELECT privilege for any table whose values are read in the condition.

Parameters

table

The name (optionally schema-qualified) of an existing table.

condition

A value expression that returns a value of type boolean that determines the rows which are to be deleted.

Outputs

On successful completion, a DELETE command returns a command tag of the form

DELETE count

The count is the number of rows deleted. If count is 0, no rows matched the condition (this is not considered an error).

Examples

Delete all films but musicals:

DELETE FROM films WHERE kind <> 'Musical';

Clear the table films:

DELETE FROM films;

Compatibility

This command conforms to the SQL standard.

Comments


Aug. 3, 2004, 2:26 a.m.

It seems that postgres supports rferring to tables other than the FROM table in the WHERE clause. This allows for much more efficient deletes is some situations. eg:

DELTE from table1 WHERE table1.foo = table2.bar

This seems to be related to \"Ommited FROM Clauses\" as per the SELECT manual page, but this feature should be made clear on this page as it is a very useful extenstion to standard SQL.
(and on the UPDATE page?)


June 2, 2005, 2:46 p.m.

Further to the above comment you can also do

DELETE FROM table1 WHERE foo IN (SELECT foo FROM table2 WHERE barone&lt;=150 AND bartwo&gt;300 and date&lt;='2005/01/01');

ie the WHERE clause can be more complex


Jan. 30, 2006, 3:35 a.m.

At least in my tests PostgreSQL doesn't allow to reference other tables as Sam posted above (DELETE FROM table1 WHERE table1.foo = table2.bar). When I try this I get the error "missing FROM-clause entry for table table2". But you can use the "WHERE foo IN", as posted by Hilary.

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group