DROP DATABASE fails with Error dropping database (can't rmdir)
ERROR 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.
Fix it fast
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/shop2Match what you see, then jump to its 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/shop2Fix
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/shopConfirmed by dev.mysql.com
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 | head2Fix
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fixDatabases
Locked out of a fresh MariaDB install as root on Mac or Linux
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
A fresh MariaDB root account is not a normal password account.
3 causes, each with a fixDatabases
An Aria table fails with 'Read page with wrong checksum' (error 1030, code 176)
ERROR 1030 (HY000): Got error 176 "Read page with wrong checksum" from storage engine Aria
Code 176 means MariaDB's Aria engine read a data or index page whose checksum does not match, so that table file is damaged.
3 causes, each with a fixDatabases
The client cannot open a TCP connection to MariaDB or MySQL (error 2003)
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
Error 2003 means the client tried a TCP connection to the host and port and nobody answered.
4 causes, each with a fix