DROP TABLE permanently removes one or more tables and all their data from a PostgreSQL database.
Use DROP TABLE when you need to permanently delete a table, its data, indexes, constraints, and dependent objects. It frees storage and removes obsolete structures.
DROP TABLE [IF EXISTS] table_name [, ...] [CASCADE | RESTRICT];
Run DROP TABLE IF EXISTS "Customers";
to safely delete one table even if you are unsure it exists.
List them comma-separated: DROP TABLE Orders, OrderItems;
Add CASCADE
: DROP TABLE Products CASCADE;
also removes foreign-key references, views, and triggers.
CASCADE deletes dependent objects automatically—handy during dev or schema refactors. RESTRICT (default) blocks the drop if dependencies exist—safer for prod.
If you archive orders elsewhere, remove the live table: DROP TABLE IF EXISTS Orders CASCADE;
. All related OrderItems
rows vanish too because of CASCADE
.
1) Always back up first. 2) Run in a transaction and verify with ROLLBACK
before COMMIT
. 3) Use IF EXISTS
to avoid errors in automated scripts.
See below for quick fixes.
No. It runs inside the current transaction. Wrap in BEGIN
…COMMIT
to control rollbacks.
Only from backups or point-in-time recovery. DROP TABLE is irreversible once committed.