Solving drop database error: can’t rmdir in MySQL

Sometimes, the only thing that we want is to drop a database(not in PRODUCTION) and recreate it from scratch, especially when we are in the process of automating the deployment of our MySQL databases, for instance using liquibase. Therefore, it is not uncommon in an initial testing process to wish to drop and recreate our tested database.

Recently, I found the following error message after to try execute a simple and rarely applied DROP DATABASE statement:

ERROR 1010 (HY000): Error dropping database (can't rmdir './db_test', errno: 39)

The reasons behind this error could be many, in my case, it was a problem related to an existing connection that was using the target database, in which case the showprocess list is your best friend, once that you have killed the process which was pointing out to the DB, the next step consists in check the data directory, the easy way is inside MySQL execute the following command:

SHOW VARIABLES WHERE Variable_name LIKE '%datadir%';

Once that you check into the datadir, you need to verify that exists a folder with the same name than the database that you want to drop, localice and inside of this folder check that not exists any file with the extensions:

  • .BAK
  • .DAT
  • .HSH
  • .MRG
  • .MYD
  • .MYI
  • .cfg
  • .db
  • .ibd
  • .ndb

As the official MySQL documentation says: “If other files or directories remain in the database directory after MySQL removes those just listed, the database directory cannot be removed”. So, you only need to be sure of having enough permission for removing any file which has the extensions mentioned above and remove it manually, to do this you only need to and issue the DROP DATABASE statement again.

I hope this simple trick would be useful, especially in lower environment and during testing automation process.

geohernandez

Share
Published by
geohernandez

Recent Posts

Formatting our Postgres scripts with pgformatter in DBeaver

Are you a PostgreSQL enthusiast using DBeaver on a Windows Platform? If you find yourself…

4 months ago

Looking back to Kimball’s approach to Data Warehousing

Over time, it's fascinating to witness how certain concepts, approaches, or visions age. Time, in…

5 months ago

List Comprehension and Walrus operator in Python

When we are working with lists, dictionaries, and sets in Python, we have a special…

9 months ago

Playing with some Pandas functions and Airflow operators

Recently, I was dealing with a task where I had to import raw information into…

11 months ago

Using interpolated format strings in Python

The release of Python 3.6 came with exciting functionalities. I want to speak about the…

1 year ago

Getting the last modified directory with Python

Working with os paths is a prevalent task, especially when working on a console application…

1 year ago