How to Delete Rows in PostgreSQL

Galaxy Glossary

How do I delete rows in PostgreSQL?

DELETE removes one or more rows from a PostgreSQL table according to an optional WHERE condition.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

Why use DELETE in PostgreSQL?

DELETE quickly removes unwanted data, reclaims storage, and keeps tables clean. Pair it with WHERE to target only the rows you need.

What is the basic DELETE syntax?

Use DELETE FROM table_name WHERE condition;. Omit WHERE to delete every row, or add RETURNING to see affected rows.

How do I delete a single row?

Filter by a unique column.

DELETE FROM users WHERE id = 42 RETURNING *;

How do I delete multiple rows with a condition?

Combine predicates with AND/OR.

DELETE FROM orders
WHERE status = 'expired' AND created_at < NOW() - INTERVAL '30 days';

How can I delete using another table?

JOIN the target and source tables.

DELETE FROM sessions s
USING users u
WHERE s.user_id = u.id AND u.disabled = true;

How do I delete all rows fast?

Use TRUNCATE table_name; for a bulk, logging-minimal operation. It is faster than DELETE without WHERE but cannot be rolled back across different sessions until commit.

How do I limit delete size to avoid locks?

Batch with LIMIT or CTE.

WITH del AS (
DELETE FROM events
WHERE processed = true
LIMIT 1000
RETURNING 1)
SELECT count(*) FROM del;

What are best practices for safe deletes?

1) Always test with SELECT first.
2) Wrap in a transaction so you can ROLLBACK.
3) Keep indexes on filter columns for speed.
4) Use CASCADE rules to manage child rows.

What mistakes should I avoid?

Deleting without WHERE, ignoring foreign-key constraints, and running massive deletes in a single transaction that bloat WAL or lock tables.

Why How to Delete Rows in PostgreSQL is important

How to Delete Rows in PostgreSQL Example Usage


DELETE FROM inventory WHERE quantity = 0 RETURNING id, product_code;

How to Delete Rows in PostgreSQL Syntax


DELETE [FROM] <table_name>
    [USING <other_tables>]
    [WHERE <condition>]
    [RETURNING * | column_list];

Common Mistakes

Frequently Asked Questions (FAQs)

Does DELETE free disk space immediately?

Not entirely. Rows become dead tuples, reclaimed later by autovacuum. To force space reuse, run VACUUM (FULL) or use TRUNCATE.

Is DELETE slower than TRUNCATE?

Yes. DELETE logs each removed row and fires triggers, while TRUNCATE is bulk and almost instantaneous but less granular and cannot activate row-level triggers.

Can I undo a DELETE?

Yes, if executed inside an uncommitted transaction. Issue ROLLBACK before commit to restore the data.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo