1650
MySQLERRORCommonData IntegrityHIGH confidence

Cannot truncate table referenced by a foreign key

Production Risk

High — truncating parent before child can orphan child rows.

What this means

TRUNCATE TABLE was attempted on a table that is referenced by a foreign key constraint from another table. InnoDB prevents this to maintain referential integrity.

Why it happens
  1. 1A child table has a foreign key pointing to the table you are trying to truncate.
  2. 2FOREIGN_KEY_CHECKS is ON.
How to reproduce
trigger — this will error
trigger — this will error
TRUNCATE TABLE parent_table;
-- child_table has FOREIGN KEY REFERENCES parent_table(id)

expected output

ERROR 1650 (HY000): Cannot truncate a table referenced in a foreign key constraint.

Fix 1

Truncate child tables first

Truncate child tables first
TRUNCATE TABLE child_table;
TRUNCATE TABLE parent_table;

Why this works

Removing child rows first satisfies the foreign key constraint.

Fix 2

Temporarily disable foreign key checks

Temporarily disable foreign key checks
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE parent_table;
SET FOREIGN_KEY_CHECKS = 1;

Why this works

Disabling FK checks allows the truncate to proceed without constraint validation.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 1650 ER_TRUNCATE_ILLEGAL_FK

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors