DELETE
The DELETE statement removes rows from a table.
Syntax
Description
DELETE removes rows that match the WHERE condition. If no WHERE clause is provided, all rows in the table are deleted. The table itself is not removed; use DROP TABLE to remove a table entirely.
WHERE Clause
The WHERE clause restricts which rows are deleted. Only rows where the expression evaluates to true are removed. Without a WHERE clause, every row in the table is deleted.
RETURNING Clause
The RETURNING clause causes the DELETE statement to return the values of each deleted row. The result columns can be any expression referencing the deleted row’s columns.
ORDER BY and LIMIT
The ORDER BY and LIMIT clauses restrict which rows are deleted. ORDER BY determines the order in which rows are considered, and LIMIT caps the number of rows that are actually removed. ORDER BY requires LIMIT to be present.
LIMIT accepts an optional OFFSET to skip a number of rows before applying the limit:
ORDER BY without LIMIT is not allowed on DELETE statements. If you need to delete all rows matching a condition in a specific order, provide a LIMIT value larger than the expected number of matching rows.
Examples
Delete rows matching a condition
Delete with RETURNING
Delete the oldest rows with ORDER BY and LIMIT
See Also