00:00

MySQL Drop Database

Last updated: 4 min read

DROP DATABASE permanently deletes a database together with every table, row, view, stored procedure, trigger and event in it. It is one of the most powerful and dangerous statements in SQL, so this tutorial covers both how to use it and how to use it safely.

1. What Gets Deleted

What DROP DATABASE removesBefore: the shop_test database contains tables, views, stored procedures, triggers and events. After DROP DATABASE, the database and everything inside it is gone. User accounts and their privilege rows remain.shop_test (before)tables + dataviewsprocedures & functionstriggers, eventsDROP DATABASE(after)database and all itsobjects are permanently goneUser accounts & GRANT rows remain
Figure 1: DROP DATABASE deletes every object inside the database in one go, and it cannot be undone.
  • All tables in the database and all their data and indexes.
  • Views, stored procedures, functions, triggers and events defined in it.
  • The database directory and files on disk.
  • Not deleted: user accounts, and privileges granted on the database (they remain until revoked, and would apply again if a database with the same name is created).

2. Syntax

syntax.sql
DROP {DATABASE | SCHEMA} [IF EXISTS] database_name;

DROP SCHEMA is a synonym. The statement reports how many tables and views it removed.

3. Dropping a Database

Look at what is inside shop_test first:

query.sql
SELECT table_name, table_type
FROM information_schema.tables
WHERE table_schema = 'shop_test';

Result: 3 rows

TABLE_NAMETABLE_TYPE
cheapVIEW
ordersBASE TABLE
productsBASE TABLE

Now drop it:

query.sql
DROP DATABASE shop_test;
Output
Query OK, 3 rows affected

"3 rows affected" is the number of table objects removed: the two tables and the view. The database is gone:

query.sql
SHOW DATABASES LIKE 'shop%';

Result: Empty set (0 rows)

Database (shop%)
No rows

4. Using IF EXISTS

Dropping a database that doesn't exist is an error:

query.sql
DROP DATABASE shop_test;
Output
ERROR 1008 (HY000): Can't drop database 'shop_test'; database doesn't exist

IF EXISTS turns the error into a note, which is useful in setup and clean-up scripts:

query.sql
DROP DATABASE IF EXISTS shop_test;
SHOW WARNINGS;
Output
Query OK, 0 rows affected, 1 warning

Result: 1 row

LevelCodeMessage
Note1008Can't drop database 'shop_test'; database doesn't exist

5. There Is No Undo

DROP DATABASE is a DDL statement, and MySQL commits DDL immediately. Wrapping it in a transaction doesn't help:

no-rollback.sql
START TRANSACTION;
DROP DATABASE old_reports;
ROLLBACK;
SHOW DATABASES LIKE 'old_reports';

Result: Empty set (0 rows)

Database (old_reports)
No rows
The only way back is restoring from a backup. Always take one before dropping anything that matters.

6. Safety Checklist Before Dropping

  1. Confirm the server: run SELECT @@hostname, @@port;. Many disasters happen on production while someone thought they were on a test server.
  2. Confirm the name: type it carefully; avoid copy-pasting from chat messages.
  3. Check what is inside: count tables and rows in information_schema.tables.
  4. Back it up: mysqldump -u root -p --routines --events --triggers dbname > dbname.sql.
  5. Check who uses it: look for application connections in SHOW PROCESSLIST.
  6. Restrict the privilege: give the DROP privilege only to administrators.
Terminal
# back up, then drop, then (if needed) restore
mysqldump -u root -p --routines --events --triggers --databases old_reports > old_reports.sql
mysql -u root -p -e "DROP DATABASE old_reports;"
mysql -u root -p < old_reports.sql      # restore if you change your mind

7. Privileges Needed

You need the DROP privilege on the database. A read-only user can't drop anything:

privileges.sql
CREATE DATABASE analytics;
CREATE USER 'analyst'@'localhost' IDENTIFIED BY 'An@lyst2026';
GRANT SELECT ON analytics.* TO 'analyst'@'localhost';
SHOW GRANTS FOR 'analyst'@'localhost';

Result: 2 rows

Grants for analyst@localhost
GRANT USAGE ON *.* TO `analyst`@`localhost`
GRANT SELECT ON `analytics`.* TO `analyst`@`localhost`

Logged in as analyst, the drop is refused:

as analyst
DROP DATABASE analytics;
Output
ERROR 1044 (42000): Access denied for user 'analyst'@'localhost' to database 'analytics'

8. Leftover Privileges

Privileges granted on a database survive the drop. If someone later creates a database with the same name, the old users get access again automatically. Clean them up:

cleanup.sql
DROP DATABASE analytics;
SHOW GRANTS FOR 'analyst'@'localhost';   -- the grant is still there
REVOKE SELECT ON analytics.* FROM 'analyst'@'localhost';
SHOW GRANTS FOR 'analyst'@'localhost';

Grants after DROP DATABASE

Grants for analyst@localhost
GRANT USAGE ON *.* TO `analyst`@`localhost`
GRANT SELECT ON `analytics`.* TO `analyst`@`localhost`

Grants after REVOKE

Grants for analyst@localhost
GRANT USAGE ON *.* TO `analyst`@`localhost`

9. Common Errors

ErrorCauseFix
1008: Can't drop database '…'; database doesn't existName typo or already droppedCheck with SHOW DATABASES; use IF EXISTS
1044: Access denied … to databaseMissing DROP privilegeAsk an administrator
Statement waits for a long timeAnother session holds a metadata lock (open transaction on a table)Find it in SHOW PROCESSLIST and commit or end it
Dropping mysql or sysNever do this: it breaks the serverLeave system databases alone

10. Summary

  • DROP DATABASE name; removes the database and everything in it.
  • IF EXISTS avoids an error when the database is missing.
  • It cannot be rolled back; take a backup first and double-check the server and name.
  • User accounts and database-level grants remain; revoke them if no longer needed.
  • Only trusted administrators should have the DROP privilege.