ERROR: cannot drop table ... because other objects depend on it means PostgreSQL's dependency tracking found other objects that would break if the drop went through: views, foreign keys, functions written in SQL-standard style, and so on. PostgreSQL refuses the drop by default and lists the dependents in the DETAIL line, as in this example. Read that list before you reach for CASCADE.
=> DROP TABLE products;
ERROR: cannot drop table products because other objects depend on it
DETAIL: view product_stats depends on table products
view top_products depends on view product_stats
constraint orders_product_id_fkey on table orders depends on table products
HINT: Use DROP ... CASCADE to drop the dependent objects too.
The SQLSTATE is 2BP01, dependent_objects_still_exist in the error codes table. The same code is used by a related message you get when removing a user: role "..." cannot be dropped because some objects depend on it. Both are covered below. The wording of these messages is the same from PostgreSQL 13 through 19.
How to read the DETAIL list
Every line has the form X depends on Y. The list is not limited to direct dependents. In the example, top_products does not use the table at all, but it is built on product_stats, which does, so it would go too. This is how dependency tracking works: a drop affects everything that depends on the object, recursively.
Two details from the PostgreSQL source matter on large schemas. The client gets at most 100 lines, followed by and N other objects (see server log for list). The server log always gets the complete list. If you drop several objects in one command and they are all blocked, the message becomes cannot drop desired object(s) because other objects depend on them.
To see direct dependents without trying a drop, query pg_depend. A view appears as its internal rule, so you will see lines like rule _RETURN on view product_stats:
-- direct dependents of one table
SELECT DISTINCT pg_describe_object(classid, objid, objsubid) AS dependent_object
FROM pg_depend
WHERE refclassid = 'pg_class'::regclass
AND refobjid = 'public.products'::regclass
AND deptype = 'n';
What CASCADE actually removes
DROP ... CASCADE removes every object in that list, and everything that depends on those objects. Two things surprise people:
- Foreign keys are cut, not followed. Dropping a referenced table with CASCADE removes only the foreign key constraint on the other table, not the other table itself. The DROP TABLE documentation says this explicitly. Your data in
orderssurvives, but nothing protects it from pointing at products that no longer exist. - Views are dropped whole. Including views on views, and any grants on them. If a reporting tool or another team used those views, they break silently until someone runs a query.
Since DDL in PostgreSQL is transactional, you can preview a cascade in a transaction and roll it back. The NOTICE lists the objects that would go (example output in the comments):
BEGIN;
SET LOCAL lock_timeout = '5s';
DROP TABLE products CASCADE;
-- NOTICE: drop cascades to 3 other objects
-- DETAIL: drop cascades to view product_stats
-- drop cascades to view top_products
-- drop cascades to constraint orders_product_id_fkey on table orders
ROLLBACK; -- read the list, then decide
Note the lock_timeout. DROP TABLE takes an ACCESS EXCLUSIVE lock and holds it until the transaction ends, even inside a preview. On a busy table it can wait behind a long query and block every query queued behind it. Our article on "canceling statement due to lock timeout" explains why a short timeout and a retry are safer than waiting.
If you list several objects in one DROP, CASCADE is needed only for dependencies outside that group. DROP VIEW top_products, product_stats works without CASCADE even though one depends on the other.
No error does not mean no dependents. For a function whose body is a string literal (the classic AS $$ ... $$), PostgreSQL does not look inside the body. Drop a table that such a function queries and the drop succeeds; the function fails later, at runtime. Only SQL-standard bodies (BEGIN ATOMIC ... END, PostgreSQL 14+) are parsed and tracked. Before dropping a table, search pg_proc.prosrc for its name.
"cannot alter type of a column used by a view or rule"
The same dependency tracking blocks column type changes. PostgreSQL cannot rewrite a stored view definition for a new column type, so it refuses:
=> ALTER TABLE products ALTER COLUMN price TYPE numeric(12,2);
ERROR: cannot alter type of a column used by a view or rule
DETAIL: rule _RETURN on view product_stats depends on column "price"
CREATE OR REPLACE VIEW does not help here, because it must produce the same columns with the same data types. The fix is to drop the dependent views, change the column and recreate the views, all in one transaction so no one ever sees the views missing:
-- 1. save the full definitions, with grants and comments
pg_dump --schema-only -t product_stats -t top_products shop > views.sql
-- 2. one transaction: drop, change, recreate, re-grant
BEGIN;
SET LOCAL lock_timeout = '5s';
DROP VIEW top_products, product_stats;
ALTER TABLE products ALTER COLUMN price TYPE numeric(12,2);
CREATE VIEW product_stats AS ...; -- from views.sql
CREATE VIEW top_products AS ...;
GRANT SELECT ON product_stats, top_products TO reporting;
COMMIT;
Use pg_dump --schema-only rather than copying the view text by hand: it also saves the grants, comments and owner, which are lost when a view is dropped. Recreate views in dependency order, base views first.
"role cannot be dropped because some objects depend on it"
A role can own tables, schemas, functions and even whole databases, and it can hold privileges on other people's objects. All of that must go before DROP ROLE works. The message lists the objects in the database you are connected to and only counts objects in other databases:
=> DROP ROLE old_app;
ERROR: role "old_app" cannot be dropped because some objects depend on it
DETAIL: owner of table orders
privileges for table customers
3 objects in database analytics
That last line is the trap: you cleaned up the current database, but the role also owns something in analytics. Ownership and privileges are recorded cluster-wide in pg_shdepend, so one query shows every database at once:
-- where does the role still own or hold something? (any database)
SELECT coalesce(d.datname, '(shared object)') AS database,
s.deptype, -- o = owner, a = privileges, r = policy
count(*)
FROM pg_shdepend s
LEFT JOIN pg_database d ON d.oid = s.dbid
WHERE s.refclassid = 'pg_authid'::regclass
AND s.refobjid = 'old_app'::regrole
GROUP BY 1, 2
ORDER BY 1, 2;
The documented recipe for dropping roles is two commands per database, in this order:
-- run in EVERY database where the role owns objects or has privileges
REASSIGN OWNED BY old_app TO app_owner;
DROP OWNED BY old_app;
-- then, once, from any database
DROP ROLE old_app;
- REASSIGN OWNED transfers ownership of everything the role owns in the current database, plus shared objects such as databases and tablespaces. It does not touch privileges the role holds on objects owned by others, or default privileges.
- DROP OWNED drops every object the role still owns in the current database and revokes its privileges, including default privileges. It does not drop databases or tablespaces.
The order is not optional. DROP OWNED without a prior REASSIGN OWNED drops the role's tables, with their data. Run REASSIGN OWNED first, in every database, and check the pg_shdepend query again before DROP OWNED.
On large production schemas
On a test database with three tables, a drop is instant and the dependency list fits on the screen. On a production cluster with thousands of objects and constant traffic, plan for these:
- Locks on every dropped object. A cascade,
DROP SCHEMA ... CASCADEorDROP OWNEDlocks every relation it removes until commit. With thousands of tables or partitions in one transaction, this can hit the lock table limit and fail with "out of shared memory". See our article on max_locks_per_transaction. In our experience, splitting the cleanup into batches is safer than raising the limit for a one-off job. - The column rewrite, not the views, takes the time. Changing a column type normally rewrites the whole table and its indexes under an exclusive lock, unless the old type is binary coercible to the new one (for example,
varchartotext). On a big table, add a new column, backfill it in batches, then swap it in a short transaction that also recreates the views. - Materialized views are rebuilt from scratch. Recreating a dropped materialized view runs its query again. On large data this can take as long as the original build, so time it on a copy first.
- Long DETAIL lists are cut at 100 lines. Read the server log for the full list before you decide a cascade is harmless.
- Role cleanup across many databases. Script it: loop over every database that allows connections, run
REASSIGN OWNEDandDROP OWNED, then rerun thepg_shdependquery until it returns nothing.
Key takeaways
- 2BP01 means other objects still depend on the one you are dropping. The DETAIL line lists them, recursively.
- Preview a cascade inside
BEGIN ... ROLLBACKwith a shortlock_timeout. - CASCADE removes foreign key constraints but keeps the referencing tables; it removes dependent views entirely, with their grants.
- To change a column type used by views, drop the views, alter the column and recreate the views in one transaction.
- To drop a role:
REASSIGN OWNED, thenDROP OWNED, in every database, thenDROP ROLE.