When maintaining or purging relational data in MySQL and MariaDB, deleting rows from a parent table that are referenced by existing foreign keys in child tables triggers a constraint violation error:

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails

While referential integrity is essential during standard application execution, administrative migrations, database seeding, or emergency data cleanup frequently require temporarily suspending constraint validation.


Suspending Constraint Checks

To allow MySQL to delete or truncate rows without evaluating foreign key dependencies, disable FOREIGN_KEY_CHECKS in the active database session:

-- Disable foreign key checks for the current session
SET FOREIGN_KEY_CHECKS = 0;

With checks disabled, execute your deletion or cleanup statements:

DELETE FROM users WHERE account_status = 'deactivated';
-- or
TRUNCATE TABLE order_history;

Re-enabling Constraint Checks

Always restore foreign key constraint checking immediately after completing the maintenance operation to protect future transactions:

-- Re-enable foreign key checks
SET FOREIGN_KEY_CHECKS = 1;

Important Operational Considerations

[!CAUTION] Disabling FOREIGN_KEY_CHECKS can leave orphaned records in child tables if rows in referenced parent tables are removed. Ensure that child tables are cleaned up accordingly or that related records are properly reconciled before returning the database to production traffic.