mysql
database-error
errno-17
rmdir-error
troubleshooting

Error Dropping Database Can't rmdir '.test', errno 17

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Error dropping database: Can't rmdir '.test', errno 17 means MySQL could not remove the schema directory from the filesystem after drop processing started. This is usually a storage or process state issue, not a SQL syntax problem. Safe recovery requires checking active connections, directory contents, permissions, and service logs before manual deletion.

What the Error Usually Implies

Errno seventeen in this context typically indicates directory removal conflict. Common causes:

  • schema directory is not empty
  • another process holds file handles
  • unexpected files were created in the schema folder
  • filesystem permissions or mount behavior prevent removal

Treat it as a data-directory hygiene issue first.

Step 1: Locate Datadir and Inspect Schema Folder

Start by confirming the exact MySQL datadir location.

sql
SHOW VARIABLES LIKE 'datadir';

Then inspect the schema directory on disk.

bash
ls -la /var/lib/mysql/test

If you find unknown files, identify origin before deleting. Backup tooling, plugins, or manual scripts can leave artifacts that block directory removal.

Step 2: Check Active Sessions and Background Jobs

Open sessions can keep resources busy during drop operations.

sql
SHOW PROCESSLIST;

Terminate stale sessions carefully if needed.

sql
KILL 12345;

Also check for backup agents, antivirus scanners, or indexers touching datadir paths.

Step 3: Retry SQL-Level Cleanup

Before touching files manually, retry cleanup through SQL:

sql
1USE test;
2SHOW TABLES;
3DROP TABLE IF EXISTS table_one;
4DROP TABLE IF EXISTS table_two;
5DROP DATABASE test;

Dropping remaining objects can reveal exactly which file type causes failure.

Step 4: Controlled Manual Recovery

If SQL retries fail and backups are confirmed, move the directory while MySQL is stopped.

bash
sudo systemctl stop mysql
sudo mv /var/lib/mysql/test /var/lib/mysql/test_orphan_backup
sudo systemctl start mysql

Moving instead of deleting preserves recovery options if you discover unexpected contents later.

Step 5: Verify Ownership, Permissions, and Disk Health

Permission drift can make drop operations fail repeatedly.

bash
sudo chown -R mysql:mysql /var/lib/mysql

Review MySQL error logs for root cause details.

bash
sudo tail -n 120 /var/log/mysql/error.log

If failures continue, inspect kernel messages for filesystem issues.

bash
dmesg | tail -n 50

Do not keep retrying destructive operations without log evidence.

Post-Fix Validation Checklist

After remediation, run a quick health check:

sql
CREATE DATABASE drop_test_tmp;
DROP DATABASE drop_test_tmp;

Then confirm corresponding directory appears and disappears correctly. This validates that drop behavior is restored.

Prevention Practices

To reduce recurrence:

  • avoid manual edits inside live datadir
  • use clean service shutdown for maintenance
  • monitor DDL failures centrally
  • keep tested restore procedures
  • quarantine suspicious schema directories before deletion

Operational discipline prevents most repeat incidents.

Automation Guardrails

If you automate schema cleanup in CI or maintenance jobs, include guardrails:

  • verify target schema name against allowlist
  • confirm recent backup snapshot exists
  • block destructive operations during active backup windows
  • require explicit dry-run mode output

These checks prevent accidental production impact while troubleshooting.

Backup and Restore Readiness

Any manual datadir intervention should be paired with tested restore procedures. Teams often take backups but do not validate restore speed or completeness. Running regular restore drills ensures that emergency cleanup decisions remain reversible under time pressure.

Common Pitfalls

A common pitfall is deleting files while MySQL is still running. This can create metadata inconsistency and harder recovery.

Another issue is assuming every drop failure is SQL-level and skipping filesystem inspection.

Teams also perform permanent delete immediately instead of quarantine move, removing forensic context and rollback options.

Summary

  • Errno seventeen on drop usually means directory removal conflict in datadir.
  • Diagnose with session checks, folder inspection, and server logs first.
  • Prefer SQL cleanup before manual filesystem intervention.
  • Use stop, move, restart workflow for safer manual recovery.
  • Validate drop behavior after fix and add operational guardrails to prevent recurrence.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.