Incident Response

PostgreSQL: cannot drop because other objects depend on it

You tried to drop a table, a column type or a role, and PostgreSQL refused because something still points at it. Here is how to read the DETAIL list, what CASCADE really removes, how to change a column used by a view, and how to remove an old role cleanly across all databases.

· 14 min read · AG Data Team

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:

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;

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:

Key takeaways

PostgreSQL Errors DDL Roles
Work with us

Need to clean up a schema or old roles safely?

We plan and run PostgreSQL schema changes and role cleanups on large production databases, with rollback plans and minimal locking.

Get in touch More from the blog