This is weird and I haven't ever encountered this.

I'm simply dropping a whole database.

$ mysql -u $RDS_USER -p$RDS_PASS -h $RDS_HOST -e "DROP DATABASE dev_test"

And yet it fails with this...

ERROR 1217 (23000) at line 1: Cannot delete or update a parent row: a foreign key constraint fails

I then go to the database itself and I see that most of the tables have been dropped except for two.

Why does this happen?

I'm using MySQL 5.6.


Try the DROP DATABASE a second time. Since most tables are gone, that may have relieved enough constraints to allow the last two tables to go.

Or... Do those two tables have FKs to each other? In this case, neither can be dropped "first". Hence, Greenforest35's answer (turn off FKs) seems to be the only solution.

Consider filing a bug report with http://bugs.mysql.com saying explaining that DROP DATABASE should be smarter -- such as ignoring the FKs automatically. (Or at least the ones that will be removed later by later tables -- this would take some analysis.)

Worse yet... Are there constraints between your remaining tables and table(s) in other database(s)?

(So many reasons not to bother with FKs.)


you should just drop all the foreign key constraints

alter table test drop foreign key testconstraint
  • 2
    I want to drop the ENTIRE database. Do I have to drop everything inside first? – dashmug Jan 12 '18 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.