Incident Response

MySQL ERROR 1451: Cannot delete or update a parent row

Child rows still point at the row you are deleting or re-keying. How to find them, when REPLACE, TRUNCATE and DROP hit the same wall, and how to clean up parents on large tables without one giant transaction.

· 12 min read · AG Data Team

MySQL ERROR 1451: Cannot delete or update a parent row means InnoDB refused to delete a row, or change its key, because rows in another table still reference it through a foreign key.

mysql> DELETE FROM customers WHERE id = 1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
(`shop`.`orders`, CONSTRAINT `fk_orders_customer` FOREIGN KEY (`customer_id`)
REFERENCES `customers` (`id`))

The server error reference lists it as ER_ROW_IS_REFERENCED_2, SQLSTATE 23000, with a short instruction: delete the children first. The part in parentheses is the useful bit. It names the child table (orders), the constraint and the columns, so you already know where the blocking rows live. It is the mirror image of ERROR 1452 Cannot add or update a child row, where the child references a parent that does not exist.

Why MySQL refuses

A foreign key declared without an ON DELETE or ON UPDATE clause behaves as RESTRICT. According to the foreign key documentation, NO ACTION is the same thing in InnoDB: the operation is rejected immediately if a matching child row exists. Unlike some other databases, InnoDB has no deferred checking, so you cannot delete the parent first and fix the children before commit. The order inside the transaction matters.

Statements that hit 1451 unexpectedly

How to find the blocking rows

For a single failing statement the error text is enough. For a cleanup job you want the full list of child tables and their rules. Run this before writing purge logic:

-- which tables point at customers, and what do they do on delete?
SELECT rc.TABLE_NAME, rc.CONSTRAINT_NAME, k.COLUMN_NAME,
       rc.DELETE_RULE, rc.UPDATE_RULE
FROM information_schema.REFERENTIAL_CONSTRAINTS rc
JOIN information_schema.KEY_COLUMN_USAGE k
  ON k.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
 AND k.CONSTRAINT_NAME  = rc.CONSTRAINT_NAME
 AND k.TABLE_NAME       = rc.TABLE_NAME
WHERE rc.CONSTRAINT_SCHEMA = 'shop'
  AND rc.REFERENCED_TABLE_NAME = 'customers';

-- which child rows block this delete?
SELECT id FROM orders WHERE customer_id = 1 LIMIT 20;

SHOW ENGINE INNODB STATUS also has a LATEST FOREIGN KEY ERROR section, described in the InnoDB monitor documentation. It shows the statement, the constraint and the parent and child index records, which helps when the failing statement came from an application and the error message was lost in a log.

How to fix it

  1. Delete children first, in the same transaction. Work from the deepest table up: line items, then orders, then the customer.
  2. Change the rule if the data model says so. If an order really has no meaning without its customer, ON DELETE CASCADE is honest. If orders must survive, ON DELETE SET NULL keeps them with an empty reference (the column must allow NULL). Changing the rule means dropping and re-creating the constraint.
  3. Soft delete. For customers, users and products, a deleted_at column is often a better answer than a delete: history and invoices keep pointing at a real row.
  4. Replace REPLACE. Use INSERT ... ON DUPLICATE KEY UPDATE for upserts on parent tables.
-- children first, then the parent, in one transaction
START TRANSACTION;
DELETE FROM order_items WHERE order_id IN (SELECT id FROM orders WHERE customer_id = 1);
DELETE FROM orders WHERE customer_id = 1;
DELETE FROM customers WHERE id = 1;
COMMIT;

-- or change the rule (drop and re-create the constraint)
ALTER TABLE orders DROP FOREIGN KEY fk_orders_customer;
ALTER TABLE orders ADD CONSTRAINT fk_orders_customer
  FOREIGN KEY (customer_id) REFERENCES customers (id)
  ON DELETE CASCADE;

Be careful with CASCADE and with switching checks off. The documentation notes that cascaded foreign key actions do not activate triggers, so audit or cleanup logic written as AFTER DELETE triggers on the child table silently does not run for cascaded rows. SET foreign_key_checks = 0 makes the 1451 go away, but the parent is deleted and the children stay as orphans. The documentation is explicit that re-enabling the variable does not scan the table data, so nothing will tell you later. Use it for reloading dumps and dropping tables, which is what it is meant for, not to get past a delete.

Purging parents on large tables

A cleanup that works on a test copy can hurt production once the tables are big:

-- delete children in small chunks, repeat until 0 rows affected
DELETE FROM orders
WHERE customer_id IN (SELECT id FROM customers_to_purge)
ORDER BY id
LIMIT 5000;

-- then the parents, also in chunks
DELETE FROM customers
WHERE id IN (SELECT id FROM customers_to_purge)
ORDER BY id
LIMIT 5000;

The batch size depends on row width, indexes and replica capacity; in our experience a few thousand rows per statement, with a short pause and a replica lag check between batches, keeps both the primary and the replicas responsive.

Key takeaways

MySQL Errors Foreign keys InnoDB
Work with us

Cleanup jobs blocked by foreign keys?

We map the foreign keys in your MySQL schema, design safe purge jobs for large tables and change constraint rules without downtime.

Get in touch More from the blog