# PostgreSQL DROP TABLE CASCADE: What It Drops, and What It Leaves Broken

> `DROP TABLE ... CASCADE` does not delete the rows that reference the table. It drops the objects that depend on the table: foreign key constraints, views and views on those views, materialized views, policies, `BEGIN ATOMIC` functions, tables that inherit from it, columns typed with its row, and defaults that use its sequences. PostgreSQL lists them only in a `NOTICE`. Run the drop without `CASCADE` first, or inside a transaction, and read the list.

Source: https://schemity.com/blog/postgres-drop-table-cascade-what-it-drops/

`DROP TABLE ... CASCADE` in PostgreSQL does not delete the rows in tables that reference it: it drops the database objects that depend on the table, and leaves those rows where they are. The danger is that the list of objects is longer than the foreign keys people expect, it can include another table and another table's column, and PostgreSQL reports it only as a `NOTICE` after the fact.

Everything below ran on PostgreSQL 18.3 in a throwaway container. The table being dropped is `customers` (1,000 rows of its own), with one of each kind of thing that can depend on a table:

```sql
CREATE TABLE customers (
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    email text NOT NULL UNIQUE,
    tier text NOT NULL DEFAULT 'free'
);
CREATE TABLE orders (
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    customer_id bigint NOT NULL REFERENCES customers (id) ON DELETE CASCADE,
    total numeric(12,2) NOT NULL
);
CREATE TABLE order_items (
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    order_id bigint NOT NULL REFERENCES orders (id) ON DELETE CASCADE,
    sku text NOT NULL
);
CREATE VIEW customer_totals AS
    SELECT c.id, c.email, sum(o.total) AS total
    FROM customers c JOIN orders o ON o.customer_id = c.id GROUP BY c.id, c.email;
CREATE VIEW big_spenders AS SELECT * FROM customer_totals WHERE total > 1000;
CREATE MATERIALIZED VIEW customer_tiers AS SELECT tier, count(*) FROM customers GROUP BY tier;
CREATE FUNCTION customer_email(p_id bigint) RETURNS text LANGUAGE sql
    AS 'SELECT email FROM customers WHERE id = p_id';
CREATE FUNCTION customer_email_atomic(p_id bigint) RETURNS text LANGUAGE sql
    BEGIN ATOMIC SELECT email FROM customers WHERE id = p_id; END;
CREATE FUNCTION touch() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RETURN NEW; END $$;
CREATE TRIGGER customers_touch BEFORE UPDATE ON customers FOR EACH ROW EXECUTE FUNCTION touch();
CREATE TABLE vip_customers (discount int NOT NULL DEFAULT 10) INHERITS (customers);
CREATE TABLE customer_snapshots (taken_at timestamptz NOT NULL DEFAULT now(), snapshot customers);
CREATE SEQUENCE customer_ref_seq OWNED BY customers.email;
CREATE TABLE tickets (id bigint PRIMARY KEY DEFAULT nextval('customer_ref_seq'), subject text NOT NULL);
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
CREATE POLICY orders_paying ON orders USING (EXISTS (
    SELECT 1 FROM customers c WHERE c.id = customer_id AND c.tier <> 'free'));
```

`orders` and `order_items` hold 10,000 rows each, `vip_customers` holds one row and `customer_snapshots` five. Half the customers are on a paid tier, so the policy shows 5,000 orders to a role `app` that has `SELECT` on `orders` and `customers`.

## Postgres DROP TABLE CASCADE: what does it drop?

It drops every object that depends on the table, and every object that depends on those, and here that was nine objects besides the table.

```
BEGIN;
DROP TABLE customers CASCADE;
NOTICE:  drop cascades to 9 other objects
DETAIL:  drop cascades to column snapshot of table customer_snapshots
drop cascades to constraint orders_customer_id_fkey on table orders
drop cascades to view customer_totals
drop cascades to view big_spenders
drop cascades to materialized view customer_tiers
drop cascades to function customer_email_atomic(bigint)
drop cascades to table vip_customers
drop cascades to default value for column id of table tickets
drop cascades to policy orders_paying on table orders
```

The `NOTICE` is a flat list, but the dependencies are not. Drawn as a tree, three of the nine are reached through something other than the table itself:

```
customers
├── constraint orders_customer_id_fkey   (on orders)
├── view customer_totals
│   └── view big_spenders                (reads only customer_totals)
├── materialized view customer_tiers
├── function customer_email_atomic(bigint)
├── table vip_customers                  (INHERITS)
├── type customers                       (the table's row type)
│   └── column customer_snapshots.snapshot
├── sequence customer_ref_seq            (OWNED BY customers.email)
│   └── default of tickets.id
└── policy orders_paying                 (on orders, reads customers)
```

| Dependent | What `CASCADE` did | What you lose |
|---|---|---|
| Foreign key `orders_customer_id_fkey` | Dropped the constraint | The check; `orders` kept all 10,000 rows |
| View `customer_totals` | Dropped | The view |
| View `big_spenders`, which reads only the other view | Dropped | The view |
| Materialized view `customer_tiers` | Dropped | The view and its stored result |
| Function with a `BEGIN ATOMIC` body | Dropped | The function |
| Function with a string body | Kept | Nothing yet: it fails on its next call |
| Table `vip_customers`, which inherits from `customers` | Dropped | The table and its rows |
| Column `customer_snapshots.snapshot`, typed `customers` | Dropped | The column and the 5 values in it |
| Default of `tickets.id`, on a sequence `customers` owns | Dropped | The default; the next insert fails |
| Policy `orders_paying` on `orders` | Dropped | The policy; `orders` reads as empty to every role but its owner |
| Trigger `customers_touch` on `customers` | Went with the table | Nothing extra; its function `touch()` stays |

Without `CASCADE`, the same statement is refused and names the same nine objects in the error's `DETAIL`. That refusal is the default, and writing `RESTRICT` says it out loud.

## Does DROP TABLE CASCADE delete rows in other tables?

No: it removes the foreign key constraint and leaves the referencing rows in place.

After the drop, `orders` and `order_items` still held 10,000 rows each, and `orders.customer_id` was a plain column full of ids that pointed at nothing. The `ON DELETE CASCADE` on that key did not run, because no row was deleted: the table was removed.

Three statements you might run against `customers` carry the word cascade, and they do three different jobs:

| Statement | What cascades | Result on `orders` here |
|---|---|---|
| `DROP TABLE customers CASCADE` | Dependent objects | Foreign key dropped, 10,000 rows kept |
| `TRUNCATE customers CASCADE` | The truncate, to referencing tables | Emptied, and `order_items` with it |
| `DELETE FROM customers` with `ON DELETE CASCADE` | The delete, row by row | Matching rows deleted, here all of them |

A plain `TRUNCATE customers` is refused while a foreign key references the table, with the hint `Truncate table "orders" at the same time, or use TRUNCATE ... CASCADE.`

Three of the nine objects do take stored data with them, and none is a foreign key: the inheriting table `vip_customers` with its rows, the `snapshot` column with its values, and the materialized view with its stored result, the only one of the three you can rebuild.

## What is left broken after the drop?

Three things kept existing and stopped working: a function, an insert, and a whole table's visibility.

The string-bodied function is not in the `NOTICE`, because PostgreSQL does not track what a string body reads. It fails when called:

```sql
SELECT customer_email(1);
-- ERROR:  relation "customers" does not exist
```

`tickets.id` lost its default, so the insert that never named the column now fails:

```sql
INSERT INTO tickets (subject) VALUES ('after');
-- ERROR:  null value in column "id" of relation "tickets" violates not-null constraint
```

The sequence itself, `customer_ref_seq`, is dropped as well and is not in the `NOTICE`: only the default that used it is named. Putting the default back means a new sequence started above the highest `id`.

The policy is the quiet one. `orders` still has row-level security enabled, and with its only policy gone the default is to show nothing. A role that could read 5,000 orders before the drop read 0 after it, with no error.

## How do you see the list before you run it?

Run the drop without `CASCADE`, or run it inside a transaction and roll back.

```sql
DROP TABLE customers;
-- ERROR:  cannot drop table customers because other objects depend on it
-- DETAIL:  column snapshot of table customer_snapshots depends on type customers
-- constraint orders_customer_id_fkey on table orders depends on table customers
-- ...
```

The refusal lists what `CASCADE` would take, with the reason for each, including objects reached through another object (`view big_spenders depends on view customer_totals`). Because DDL is transactional in PostgreSQL, the other way is `BEGIN; DROP TABLE customers CASCADE;`, read the `NOTICE`, then `ROLLBACK`. All 1,000 customers were back after the rollback. Note that the drop holds an `ACCESS EXCLUSIVE` lock on the table and on every table it touches (here `orders`, `tickets`, `customer_snapshots` and `vip_customers`) until the transaction ends, so reads of `orders` wait too. Do not leave it open on a live database.

The client sees at most 100 dependents. Past that the message ends `and N other objects (see server log for list)`, and the full list is in the server log.

Neither list includes the string-bodied function. Find those by searching function bodies for the table name, and treat a match as a maybe.

The cautious version of the migration drops each dependent by name, in order, and ends with a `DROP TABLE` that has no `CASCADE`. If something new has come to depend on the table since you wrote it, the last statement fails and tells you.

On a production database, many teams split the drop over two releases: rename the table first (`ALTER TABLE customers RENAME TO customers_retired`), and drop it a release later. Views, policies and foreign keys follow a rename, because PostgreSQL holds them by object id, not by name. Anything that names the table as text, the string-bodied function and the application's own queries, fails right away, and one more `RENAME` undoes it. A `DROP` cannot be undone that way.

## Is DROP TABLE CASCADE the same on MySQL, SQL Server and SQLite?

No, and none of them drops dependent objects the way PostgreSQL does.

| | `DROP TABLE t CASCADE` | A foreign key into `t` | A view on `t` |
|---|---|---|---|
| PostgreSQL 18.3 | Drops dependents | Constraint dropped | Dropped |
| MySQL 8.4.11 | Keyword accepted and ignored | Drop refused, error 3730 | Left broken, error 1356 on `SELECT` |
| SQL Server 2022 | Syntax error, Msg 156 | Drop refused, Msg 3726 | Left broken, Msg 208, or refuses the drop if schema-bound (Msg 3729) |
| SQLite 3.54.0 | Syntax error | See below | Left broken, "no such table" |

SQLite is the one that touches rows. With `PRAGMA foreign_keys = ON`, a plain `DROP TABLE customers` deletes the table's rows first, so the foreign key actions run: a child table with `ON DELETE CASCADE` was emptied, and one with the default `NO ACTION` and matching rows stopped the drop with "FOREIGN KEY constraint failed". That is the opposite of PostgreSQL, where the same key on the same tables kept every child row.

## How does Schemity show what a table drop takes with it?

Schemity lists each dependent of the table before the migration runs, with what PostgreSQL will do to it.

Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. When you delete a table on a connected PostgreSQL diagram, the planned migration drops the foreign keys the diagram knows by name and then writes `DROP TABLE IF EXISTS "customers" CASCADE`. So what the `CASCADE` reaches is exactly the part worth reading, and [impact analysis](https://schemity.com/doc/impact-analysis/) (Schemity Pro) puts it under Dependent objects: the views, including a view that reads the table only through another view, the materialized view, the policy, the `BEGIN ATOMIC` function, the inheriting table, the typed column and the other table's default, each with "CASCADE drops it too, and the database reports that only as a notice." The string-bodied function is listed as one that mentions the table and may fail the next time it runs. This is the list you would otherwise get from the `NOTICE`, read before the statement instead of after, which is the point of being able to see the impact of every change.

![Schemity's change preview for dropping customers on PostgreSQL 18.3: customers outlined in red with its line to orders, the planned migration ending in DROP TABLE IF EXISTS "customers" CASCADE, and the impact findings. "10 objects depend on customers" lists materialized view customer_tiers, view customer_totals, view big_spenders, function customer_email_atomic, inheriting table vip_customers, column customer_snapshots.snapshot, default of column tickets.id and policy orders_paying, each with "CASCADE drops it too, and the database reports that only as a notice", plus trigger customers_touch, dropped with the table, and function customer_email, which mentions customers in its body.](https://schemity.com/images/blog/postgres-drop-table-cascade-what-it-drops/impact-drop-customers-cascade.webp)

Most of these objects never appear on an ERD. A diagram draws tables, columns and foreign keys, so the views, policies, functions and sequence defaults that `CASCADE` reaches are exactly the part a diagram leaves out, the same way it leaves out [what ON DELETE CASCADE will delete](https://schemity.com/blog/on-delete-cascade-is-invisible-in-your-erd/). A foreign key from a table in [another schema](https://schemity.com/blog/postgres-schema-vs-database/) is on the same list too, since it is outside the diagram and `CASCADE` drops it as well. In a modular monolith that is often [another module's foreign key](https://schemity.com/blog/foreign-keys-between-modules-modular-monolith/), which the team dropping the table may never have seen.

The list has limits, and they matter more than the list. It covers what the database records. The string-bodied function is found by matching the table's name as a whole word in the function's source, so a function that builds the name at run time, such as `EXECUTE format('SELECT ... FROM %I', 'cust' || 'omers')`, is not found. Nothing outside the database is found at all: the application's queries, ORM models, reports, scheduled jobs and other services that read the table. Impact analysis reads the database, not your application, so an empty list means the database holds nothing else that depends on the table, not that nothing reads it. Searching the codebase, and the rename-first release above, cover the rest.

The same question for one column, where PostgreSQL drops some dependents without any `CASCADE` at all, is in [what DROP COLUMN takes with it](https://schemity.com/blog/postgres-drop-column-what-it-drops/).

## Frequently asked questions

### Does `DROP TABLE CASCADE` delete rows in other tables in PostgreSQL?

No. It drops the foreign key constraint that pointed at the table and leaves the referencing rows in place, now pointing at nothing. In the test here, `orders` kept all 10,000 rows after `DROP TABLE customers CASCADE`. The statement that empties referencing tables is `TRUNCATE ... CASCADE`, and the one that deletes matching rows is a `DELETE` with `ON DELETE CASCADE`.

### How do I see what `DROP TABLE CASCADE` will drop before running it?

Run `DROP TABLE` without `CASCADE`. PostgreSQL refuses and prints the dependent objects in the error's `DETAIL`, including the ones reached through another object (the first 100; the rest go to the server log). Or run the `CASCADE` form inside `BEGIN`, read the `NOTICE`, and `ROLLBACK`, since DDL is transactional in PostgreSQL.

### Does MySQL support `DROP TABLE CASCADE`?

It accepts the keyword and ignores it. On MySQL 8.4.11 `DROP TABLE customers CASCADE` still failed with error 3730 while a foreign key referenced the table, and once the key was gone it dropped the table and left the view that read it broken (error 1356 on the next `SELECT`).
