DELETE permanently removes selected rows from a PostgreSQL table.
The DELETE statement removes unwanted rows, freeing storage and keeping datasets accurate. Use it when soft-delete columns or archival tables are unnecessary.
Start with DELETE FROM table_name WHERE condition;
. The WHERE clause is mandatory for anything other than a full-table purge.
Only omit WHERE when you truly intend to empty the table.Always double-check by running a SELECT
first.
Yes. Execute the same condition with SELECT *
. When satisfied, change SELECT
to DELETE
.
Use USING
in DELETE.It lets you reference another table, useful for cascade-like cleanups without foreign keys.
The RETURNING
clause outputs deleted rows—ideal for audits or downstream logic.
Wrap deletions in a transaction, add indexes on filter columns, and log statements. Always test in staging first.
.
No. TRUNCATE instantly removes all rows and can't use WHERE or RETURNING. DELETE is slower but flexible and transactional.
Space becomes reusable for the same table, but disk files shrink only after VACUUM FULL or table rewrite.
Use a CTE with ROW_NUMBER, then DELETE where row_number > 1.