PostgreSQL DELETE
DELETE removes rows from a table. Like UPDATE, it uses a WHERE clause to choose the rows, and like UPDATE, forgetting the WHERE affects every row.
DELETE FROM table_name
WHERE condition;Deleting selected rows
Remove all cancelled orders. Thanks to ON DELETE CASCADE on order_items.order_id, their order lines disappear too:
SELECT count(*) AS orders, (SELECT count(*) FROM order_items) AS items FROM orders;
DELETE FROM orders WHERE status = 'cancelled';
SELECT count(*) AS orders, (SELECT count(*) FROM order_items) AS items FROM orders; orders | items
--------+-------
20 | 34
(1 row)
DELETE 2
orders | items
--------+-------
18 | 32
(1 row)
The command tag DELETE 2 says two orders were removed; their two order lines went with them.
Foreign keys can block a delete
What if you try to delete a book that appears in orders? order_items.book_id references books without ON DELETE CASCADE, so PostgreSQL protects the order history:
DELETE FROM books WHERE id = 1;
DELETE FROM books WHERE id = 2;
SELECT count(*) AS books_left FROM books;ERROR: update or delete on table "books" violates foreign key constraint "order_items_book_id_fkey" on table "order_items"
DETAIL: Key (id)=(1) is still referenced from table "order_items".
DELETE 1
books_left
------------
19
(1 row)
Book 1 has been sold, so deleting it would leave order lines pointing at nothing. Book 2 has never been sold and is removed without complaint.
ON DELETE option |
What happens to referencing rows |
|---|---|
NO ACTION / RESTRICT (default) |
the delete fails |
CASCADE |
they are deleted too |
SET NULL |
their foreign key column becomes NULL |
SET DEFAULT |
their foreign key column gets its default |
Deleting with a condition on another table: USING
PostgreSQL's DELETE … USING lets the condition refer to another table, much like UPDATE … FROM. Delete every order line for a book in the Children category:
DELETE FROM order_items oi
USING books b
WHERE b.id = oi.book_id
AND b.category_id = 6;
SELECT count(*) AS remaining_items FROM order_items;DELETE 5
remaining_items
-----------------
29
(1 row)
The same result can be written with a subquery: WHERE book_id IN (SELECT id FROM books WHERE category_id = 6).
Deleting everything: DELETE vs TRUNCATE
DELETE FROM t without WHERE empties the table row by row. TRUNCATE does the same much faster on large tables, and can also reset identity counters:
CREATE TABLE visits (id integer GENERATED ALWAYS AS IDENTITY, page text);
INSERT INTO visits (page) VALUES ('/home'), ('/books'), ('/cart');
DELETE FROM visits;
INSERT INTO visits (page) VALUES ('/home');
SELECT * FROM visits;
TRUNCATE visits RESTART IDENTITY;
INSERT INTO visits (page) VALUES ('/home');
SELECT * FROM visits;CREATE TABLE
INSERT 0 3
DELETE 3
INSERT 0 1
id | page
----+-------
4 | /home
(1 row)
TRUNCATE TABLE
INSERT 0 1
id | page
----+-------
1 | /home
(1 row)
After DELETE the counter keeps going (the new row gets id 4); after TRUNCATE … RESTART IDENTITY it starts at 1 again.
Note:
TRUNCATErefuses to empty a table that other tables reference with a foreign key, unless you addCASCADE, which truncates those tables too.
Seeing what you deleted
Add RETURNING to get the deleted rows back, a nice way to double-check:
DELETE FROM customers
WHERE id NOT IN (SELECT customer_id FROM orders)
RETURNING id, first_name, last_name; id | first_name | last_name
----+------------+-----------
7 | Gina | Rossi
13 | Mia | Fischer
15 | Olivia | Martin
(3 rows)
DELETE 3
Those are the three customers who never ordered. The DELETE 3 tag follows the returned rows.
Test yourself
What does DELETE FROM books; do?
- Drops the books table
- Deletes every row but keeps the table
- Deletes the first row
- Raises an error without WHERE
An order_items foreign key has ON DELETE CASCADE. You delete an order. What happens to its items?
- They stay
- They are deleted too
- Their order_id becomes NULL
- The delete fails
Which statement empties a table and can restart its identity counter?
- DELETE ... RESET
- DROP TABLE
- TRUNCATE ... RESTART IDENTITY
- VACUUM