Sub topic 4
Capturing Deleted Rows with `DELETE ... RETURNING`
Assume you know basic DELETE, WHERE, primary keys, and how to run SQL in psql or another PostgreSQL client. This chapter focuses on the rows PostgreSQL reports while it deletes them.
What `DELETE ... RETURNING` does
A normal DELETE reports how many rows it removed. Adding RETURNING makes PostgreSQL send the deleted rows back as a result set:
DELETE FROM tasks
WHERE status = 'done'
RETURNING id, title, status;The returned values describe each row immediately before deletion. The statement returns one result row for every deleted row, so application code can log the deleted records or use their identifiers without running a second SELECT.
If no row satisfies the WHERE condition, the statement succeeds but returns zero rows.
flowchart TD
A[DELETE finds rows matching WHERE] --> B{Rows found?}
B -->|No| C[Delete nothing and return zero rows]
B -->|Yes| D[Delete matching rows]
D --> E[Evaluate RETURNING for each deleted row]
E --> F[Send returned rows to the client]Choosing the `RETURNING` list
The list works like a SELECT list. You can choose specific columns, use expressions, or request every column with *.
DELETE FROM tasks
WHERE id = 42
RETURNING id,
title,
status,
deleted_at,
'removed' AS event_type;The expression 'removed' AS event_type adds a constant value to every result row. You can also transform columns:
DELETE FROM tasks
WHERE status = 'done'
RETURNING id, lower(title) AS normalized_title;Use table_alias.* when the target table has an alias:
DELETE FROM tasks AS t
WHERE t.status = 'done'
RETURNING t.*;RETURNING * is convenient for audit logging, but selecting only the columns you need usually keeps the result smaller and makes the client contract clearer.
Returning values from a joined delete
DELETE ... USING lets you match the rows to delete through another table. The RETURNING list can include columns from the target table and the tables used for matching.
DELETE FROM sessions AS s
USING users AS u
WHERE s.user_id = u.id
AND u.disabled_at IS NOT NULL
RETURNING s.id AS session_id,
s.user_id,
u.email AS disabled_user_email;This deletes sessions belonging to disabled users and returns enough information for an audit event. The USING table helps find rows, but PostgreSQL deletes only rows from the table named immediately after DELETE FROM, which is sessions here.
Capturing rows for another statement
A data-modifying common table expression, or CTE, can pass deleted rows into an INSERT. This lets you archive and delete rows in one statement:
WITH removed AS (
DELETE FROM tasks
WHERE status = 'done'
RETURNING id, title, status
)
INSERT INTO tasks_archive (id, title, status)
SELECT id, title, status
FROM removed;The RETURNING output from removed becomes the input to the archive insert. The whole statement runs as one database operation. If the archive insert fails, the deletion is rolled back with the statement.
Common mistakes
| Mistake | Result | Safer approach |
|---|---|---|
| Omitting `WHERE` | Every row is deleted | Test the condition with `SELECT` first |
| Expecting `RETURNING` to show a post-delete row | The row no longer exists | Treat values as the deleted row's final values before removal |
| Ignoring the result set | Useful deleted data is discarded | Read and process every returned row |
| Returning `*` for very wide rows | More data crosses the connection | Return only required columns |
RETURNING does not turn DELETE into a preview. The deletion happens when the statement runs. Use a transaction when you need to inspect the result before committing.
Exercise
What does this statement return if no task has the title "Missing"? DELETE FROM tasks WHERE title = 'Missing' RETURNING id, title;
Show answer
It returns zero result rows. The statement succeeds, but no task is deleted.