Explain it simply
DROP TABLE in plain language
DROP TABLE removes a table from the database completely. Not just the rows: the columns, indexes, constraints and permissions go with it. Once the statement finishes, no query can find that table any more.
Picture a folder in a filing cabinet. DELETE takes some or all of the sheets out of the folder. TRUNCATE empties every sheet at once but leaves the empty folder in place. DROP TABLE takes the folder itself out of the cabinet and throws it in the bin.
Problem → Why → Solution
The problem: other objects depend on the table
You want to clear out test data, so you type DROP TABLE departments. PostgreSQL refuses, because the foreign key on employees and the employee_directory view both depend on that table. The error's HINT suggests CASCADE.
Following that hint without thinking is the real danger. CASCADE drops the view along with departments and silently removes the foreign-key rule from employees as well; the only record of what went is a few NOTICE lines that nobody reads.
DROP TABLE departments;
-- ERROR: cannot drop table departments because other objects depend on it
-- DETAIL: constraint employees_department_id_fkey on table employees
-- depends on table departments
-- view employee_directory depends on table departments
-- HINT: Use DROP ... CASCADE to drop the dependent objects too.Why PostgreSQL refuses
DROP TABLE defaults to RESTRICT: if any other object depends on the table, nothing is dropped. That is protection rather than an obstacle, because it shows you dependencies you may not have known about. Choose CASCADE only after reading the DETAIL lines and accepting that every object listed there will disappear.
Core concept: what goes and what stays
The three commands that remove data serve different purposes. To pick the right one, ask two questions: should the table itself still exist afterwards, and do you want to remove only some of the rows?
| Command | Removes | Keeps |
|---|---|---|
| DELETE FROM t WHERE ... | the matching rows | the table, other rows, structure |
| TRUNCATE t | every row, quickly | structure, indexes, permissions |
| DROP TABLE t | every row and the table itself | nothing |
Three options matter for DROP TABLE. IF EXISTS turns a missing table into a NOTICE instead of an error, so a script can run twice without failing. RESTRICT, the default, refuses when dependent objects exist. CASCADE drops dependent views and removes foreign-key constraints on other tables, but it does not drop those other tables or their rows. You can also drop several tables at once with DROP TABLE a, b, c.
In PostgreSQL only the table's owner, the schema owner or a superuser may drop it, so an application login role that does not own its tables is a useful safeguard. DDL is transactional, which means a DROP TABLE inside BEGIN can still be undone with ROLLBACK before you COMMIT. After COMMIT there is no recycle bin: the table comes back only from a backup such as pg_dump or point-in-time recovery. Oracle, by contrast, moves a dropped table to a recycle bin unless you add PURGE, so behaviour differs by database.
Find dependencies before you drop
The quickest way to get the list of dependencies is to try a plain DROP TABLE, without IF EXISTS or CASCADE, inside a transaction: if it fails, the DETAIL names every object. In psql, \d+ table_name also lists the foreign keys that point at the table under 'Referenced by'.
- Is the table itself unwanted?
- List the objects that depend on it
- Confirm a backup exists
- Drop inside BEGIN, check, then COMMIT
If you only want the rows gone, you do not need DROP; TRUNCATE or DELETE keeps the structure and the permissions.
General pattern
DROP TABLE [IF EXISTS] table_name [, other_table ...] [RESTRICT | CASCADE];
-- keep the table, remove every row
TRUNCATE TABLE table_name [RESTART IDENTITY] [CASCADE];
-- keep the table, remove some rows
DELETE FROM table_name WHERE condition;Parts in square brackets are optional. RESTRICT is the default and can be left out; CASCADE only happens when you deliberately write it.
Worked example: dropping a staging table and employees
The database holds departments, employees and an import_staging table left behind by an import. employees points at departments through a foreign key, but nothing points at employees, so it can be dropped even under RESTRICT. The last query shows which tables remain.
-- Remove a leftover staging table; no error if it is already gone
DROP TABLE IF EXISTS import_staging;
-- Remove the employees table: its structure and all of its rows
DROP TABLE employees;
-- Which tables are left?
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;Code breakdown
- Idempotent cleanup
DROP TABLE IF EXISTS import_staging;Drops the table if it exists and only raises a NOTICE if it does not, so running the deploy script a second time does not fail.
- Drop with the default RESTRICT
DROP TABLE employees;The three rows, the primary-key index and the foreign-key constraint pointing at departments all go. The departments table is untouched, because the constraint belonged to employees.
- Confirm what is left
FROM information_schema.tablesLists the table names in the public schema; only departments is left. In psql, \dt shows the same list.
table_name
-----------
departmentsRehearsing a drop inside a transaction
When you are not sure what CASCADE will take with it, use PostgreSQL's transactional DDL to rehearse. Run the DROP after BEGIN, query what is left, then put everything back with ROLLBACK. In the script below, the SELECT inside the transaction lists only employees and import_staging, while the SELECT after ROLLBACK shows all three tables and the view again.
BEGIN;
DROP TABLE departments CASCADE; -- also drops the view and the FK
SELECT table_name, table_type
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;
ROLLBACK; -- nothing was really dropped
SELECT table_name, table_type
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;Mind the lock
While the transaction is open, DROP TABLE holds an ACCESS EXCLUSIVE lock, so other sessions wait even to read the table. If you rehearse on production, ROLLBACK or COMMIT quickly; better still, rehearse on a staging copy.
TRUNCATE respects foreign keys too: while employees points at departments, TRUNCATE departments fails with cannot truncate a table referenced in a foreign key constraint. You must empty both together with TRUNCATE employees, departments. Adding RESTART IDENTITY also resets identity-column counters back to 1.
In real projects
DROP TABLE in real projects
In production, DROP TABLE is rare and appears only inside reviewed migrations. When a feature is retired, most teams do not drop its table straight away: they rename it to something like feature_x_archived or move it to an archive schema, and drop it a release or two later once nobody has touched it.
- ETL and data-import jobs clear their staging tables with DROP TABLE IF EXISTS before each run and recreate them, or use CREATE TEMP TABLE, which disappears automatically at the end of the session.
- Automated test suites often reset tables between tests with TRUNCATE ... RESTART IDENTITY CASCADE, which is far faster than rebuilding the schema.
- When log or event data is partitioned by month, old data is removed by dropping the old partition instead of running DELETE, which clears millions of rows instantly.
Common mistakes
Common DROP TABLE mistakes
Using DROP when you only wanted the rows gone
Dropping and recreating a table tends to lose its GRANTs, indexes, triggers and comments along the way. If the table is still needed, use TRUNCATE or DELETE, which keep the whole structure.
Adding CASCADE blindly because the HINT said so
CASCADE drops views that reports depend on and removes foreign keys from other tables, after which orphan rows can slip in unnoticed. Reading the DETAIL list and removing each dependency yourself is safer.
Running it against the wrong database
With two terminals open, it is easy to run DROP TABLE on production while believing you are on staging. Check SELECT current_database(); first, and do not let your everyday production role own the tables.
Mini challenge
Mini challenge: retire the departments table
The database has departments, employees (with a foreign key to departments), an employee_directory view and an import_staging table. Drop departments without CASCADE by removing its dependencies yourself. The employee rows must survive.
- Do everything inside one BEGIN ... COMMIT
- Drop import_staging and the non-existent import_staging_old in one statement without an error
- Drop the view with DROP VIEW and remove the foreign key with ALTER TABLE ... DROP CONSTRAINT
- Drop departments without CASCADE, then show the remaining tables and the number of employee rows
BEGIN;
-- TODO: drop import_staging and import_staging_old in one statement;
-- one of them does not exist, and that must not be an error
-- TODO: drop the employee_directory view
-- TODO: remove the foreign key employees_department_id_fkey
-- TODO: drop departments (no CASCADE)
COMMIT;
-- TODO: list the tables in the public schema
-- TODO: count the rows in employeesExpected: Only the employees table remains, and it still holds three rows.
Show hint
IF EXISTS works with a list of table names. The constraint's name appears in the error DETAIL from the problem section. Once both dependencies are gone, a plain RESTRICT drop of departments succeeds.
Show solution
BEGIN;
DROP TABLE IF EXISTS import_staging, import_staging_old;
DROP VIEW employee_directory;
ALTER TABLE employees
DROP CONSTRAINT employees_department_id_fkey;
DROP TABLE departments; -- RESTRICT: nothing depends on it any more
COMMIT;
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;
SELECT count(*) AS employees_left FROM employees;Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- DROP TABLE deletes a table's structure and all its data; TRUNCATE and DELETE keep the table.
- IF EXISTS turns a missing table into a NOTICE, so cleanup scripts can run twice.
- The default RESTRICT refuses when objects depend on the table; CASCADE also drops dependent views and foreign-key constraints.
- In PostgreSQL a drop can be rolled back before COMMIT; after COMMIT only a backup can bring it back.
PostgreSQL SQL Tutorial — PostgreSQL Global Development Group