Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

DROP DATABASE fails with Error dropping database (can't rmdir)

ErrorERROR 1010 (HY000): Error dropping database (can't rmdir './shop', errno: 39 - Directory not empty)

Short answer

Error 1010 means MySQL deleted the table files it knows about but could not remove the database directory itself. The errno at the end says why: 39 is a directory that is not empty (a file MySQL did not create is still inside), and 13 is permission denied. Remove the stray files or fix the ownership, then run DROP DATABASE again.

  • Applies to MySQL 8.4 LTS, MySQL 8.0, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the data directory, then list what is left inside the database folder. The errno in the error message tells you which cause below applies.

    mysql -u root -p -e "SELECT @@datadir;"
    sudo ls -la /var/lib/mysql/shop
  2. 2Match what you see, then jump to its fix

    1. 1errno: 39 - Directory not emptyA file MySQL did not create is still in the database folder (errno 39)Fix
    2. 2errno: 13 - Permission deniedThe database folder or its files belong to another account (errno 13)Fix

What does the error look like?

  • ERROR 1010 (HY000): Error dropping database (can't rmdir './shop', errno: 39 - Directory not empty)
  • Error dropping database (can't rmdir '%s', errno: %d - %s)

Cause 1 of 2

A file MySQL did not create is still in the database folder (errno 39)

1Check

DROP DATABASE removes only the files MySQL itself creates (extensions such as .ibd, .MYD, .MYI, .cfg and a few more). Anything else in the folder, such as a backup copy, an editor file or a dump you put there, stops the folder from being removed. Errno 39 is ENOTEMPTY, Directory not empty.

sudo ls -la /var/lib/mysql/shop

2Fix

Move the leftover files out of the folder, then repeat the statement. Replace shop and the path with your database name and the data directory from the first step.

sudo mkdir -p /root/leftovers
sudo mv /var/lib/mysql/shop/* /root/leftovers/
mysql -u root -p -e "DROP DATABASE shop;"

3Verify

The database is gone from the list and the folder no longer exists.

mysql -u root -p -e "SHOW DATABASES;"
sudo ls -d /var/lib/mysql/shop

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 2

The database folder or its files belong to another account (errno 13)

1Check

The server must own its database folders and files to read and write them. Files copied into the data directory as root, or restored from a backup as another user, are not owned by the mysql account. Errno 13 is EACCES, Permission denied.

sudo ls -ld /var/lib/mysql/shop
sudo ls -l /var/lib/mysql/shop | head

2Fix

Give the folder and its files back to the account that runs the server, then drop the database again.

sudo chown -R mysql:mysql /var/lib/mysql/shop
mysql -u root -p -e "DROP DATABASE shop;"

3Verify

SHOW DATABASES no longer lists the database.

mysql -u root -p -e "SHOW DATABASES;"

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Never keep backups, notes or exports inside the MySQL data directory; use a separate backup folder.
  • When you copy database files in by hand, set the owner to the mysql account straight away.
  • Take dumps with mysqldump instead of copying live files.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.