ON DELETE CASCADE: review child data before deleting a parent
Learn what a foreign key cascade deletes, when dependent records should follow their parent, and how to review the impact before a real deletion.
What does ON DELETE CASCADE do?
ON DELETE CASCADE is a foreign-key rule that makes the database automatically delete child rows that reference a parent row when that parent is deleted. It can fit records such as order items that have no meaning without their order. It can also cause an unexpected bulk deletion if the relationship connects independent records, such as customers or products. Choose it only after considering the data lifecycle and the full deletion scope.
What gets deleted automatically?
For example, orders stores orders and order_items stores items belonging to each order. The foreign key below removes an order's items when the order is deleted:
CREATE TABLE orders (
order_id bigint PRIMARY KEY,
placed_at timestamptz NOT NULL
);
CREATE TABLE products (
product_id bigint PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE order_items (
order_item_id bigint PRIMARY KEY,
order_id bigint NOT NULL,
product_id bigint NOT NULL,
quantity integer NOT NULL CHECK (quantity > 0),
CONSTRAINT order_items_order_fk
FOREIGN KEY (order_id) REFERENCES orders (order_id)
ON DELETE CASCADE,
CONSTRAINT order_items_product_fk
FOREIGN KEY (product_id) REFERENCES products (product_id)
ON DELETE RESTRICT
);In this design, deleting an order removes its order_items, while deleting a product referenced by an order item is blocked. A single child table can use different policies for different relationships. PostgreSQL's documentation likewise treats order items as components that can be removed with an order, while products and orders are independent objects whose connection should not automatically erase historical order items.
How does CASCADE differ from other delete actions?
The foreign-key action expresses the lifecycle of the relationship.
| Action | When child rows reference the parent | Suitable when |
|---|---|---|
CASCADE | Deletes the children along with the parent | A child is a dependent component with no independent meaning |
RESTRICT | Rejects deletion of the referenced parent | The parent must remain while children exist |
NO ACTION | Fails if the reference still exists when the constraint is checked | You want the default or explicitly account for deferred checking where supported |
SET NULL | Keeps each child and sets its foreign-key value to NULL | The relationship is optional and the child should remain |
For SET NULL, the child foreign-key column must allow NULL. The details of NO ACTION and RESTRICT vary by DBMS. PostgreSQL can defer a NO ACTION constraint check until the end of a transaction in some cases, whereas RESTRICT blocks the deletion immediately. In MySQL 8.4 InnoDB, both names have the same immediate-rejection behavior. Check the documentation for the engine and version you use if this difference matters.
How can you review the deletion scope first?
Before designing the schema or deleting production data, inspect the number of child rows and confirm that the parent is the intended target. This illustrative query uses PostgreSQL syntax:
SELECT o.order_id, count(oi.order_item_id) AS item_count
FROM orders AS o
LEFT JOIN order_items AS oi ON oi.order_id = o.order_id
WHERE o.order_id = 9001
GROUP BY o.order_id;If the example returns four items, deleting order 9001 will also remove those four rows. If accounting or audit rules require keeping them, CASCADE may not fit. Before an actual deletion, check the environment, predicate, expected row count, backup and recovery path, and approval process. Where possible, review the result in a transaction before committing. Verify transaction and locking behavior for your DBMS and operational setup.
Boundaries that are easy to miss
First, ON DELETE CASCADE responds to an actual parent-row DELETE. A soft delete that only changes a column such as deleted_at does not invoke the foreign-key delete action. Soft deletion or a separate archive policy may fit data with retention requirements, but then the application or a separate process must keep deletion markers and dependent data consistent.
Second, in a schema with several foreign keys, one deletion can affect connected child records. Inspect every action along the relationship path. MySQL 8.4 documents that cascaded foreign-key actions do not activate triggers. An audit design that relies only on child-row delete triggers may therefore miss the expected records in MySQL. Check engine-specific behavior and implement auditing through a separately verified approach.
Choosing an action
Consider `CASCADE` when a child is a dependent component that should be deleted with its parent.
Prefer considering `RESTRICT` or `NO ACTION` when the child is an independent record or must be retained.
Consider a nullable foreign key with `SET NULL` when the relationship can end while the child remains.
If soft deletion, auditing, or legal retention is required, decide first whether physical deletion and cascading fit those requirements.
Before a production delete, inspect the foreign-key definitions and expected child count, and prepare a recovery path.
Sources and scope
[PostgreSQL 18: Constraints — foreign keys and referential actions](https://www.postgresql.org/docs/18/ddl-constraints.html)
[MySQL 8.4: FOREIGN KEY constraints — referential actions](https://dev.mysql.com/doc/refman/8.4/en/create-table-foreign-keys.html)
Documentation checked on November 23, 2026. The SQL illustrates behavior and was not executed. Support and details of delete actions vary by DBMS, storage engine, and version.