The basic command to delete a database in MySQL
To delete a database in MySQL, you use the DROP DATABASE command followed by the database name. The full syntax is DROP DATABASE database_name; — replace database_name with the actual name of the database you want to remove.
MySQL will delete the database and all tables, data, and stored procedures inside it. This action cannot be undone, so verify the database name before you run the command. Once executed, the database is gone permanently.
Key Takeaways
- The command DROP DATABASE database_name; removes a database and everything in it permanently.
- You must have the DROP privilege on that database, or MySQL will reject the command with a permission error.
- Use DROP DATABASE IF EXISTS database_name; to avoid an error if the database does not exist.
- Always double-check the database name before running DROP DATABASE, because the deletion cannot be reversed.
- Some hosting providers restrict DROP DATABASE or require you to delete databases through a control panel instead of the command line.
Step-by-step: deleting a database from the MySQL command line
First, open a terminal or command prompt and connect to your MySQL server. Type mysql -u username -p and press Enter, then enter your password when prompted. Replace username with your actual MySQL username.
Once you are logged in, you will see the mysql> prompt. Type DROP DATABASE database_name; and press Enter. MySQL will confirm the deletion with a message like "Query OK, 0 rows affected." The database is now deleted.
If you want to see which databases exist before you delete one, type SHOW DATABASES; at the prompt. This lists all databases on the server so you can verify the name you are about to drop.
Using IF EXISTS to prevent errors
If you run DROP DATABASE database_name; and the database does not exist, MySQL returns an error: "Error 1008: Can't drop database 'database_name'; database doesn't exist." This stops your script or command sequence.
To prevent this error, use DROP DATABASE IF EXISTS database_name; instead. MySQL will delete the database if it exists and do nothing if it does not, without throwing an error. This is especially useful in scripts or backup procedures where you want the command to succeed whether or not the database is present.
Permission requirements and common errors
You must have the DROP privilege on the database to delete it. If you try to drop a database and you do not have this permission, MySQL returns "Error 1044: Access denied for user 'username'@'hostname' to database 'database_name'."
If you see this error, ask your database administrator or hosting provider to grant you DROP privileges, or use an account that has them. On a local server you control, you can grant the privilege yourself by running GRANT DROP ON database_name.* TO 'username'@'localhost'; as the root user.
Some hosting providers disable the DROP DATABASE command entirely for security reasons. If the command is blocked, you will see an error like "Access denied; you need the SUPER privilege for this operation." In that case, use your hosting provider's control panel (cPanel, Plesk, or similar) to delete the database instead.
Deleting a database through a hosting control panel
If you cannot use the command line, most hosting providers offer a graphical way to delete databases. In cPanel, log in, find the MySQL Databases tool, locate the database in the list, and click Delete. The control panel will ask you to confirm before removing it.
In Plesk, go to Databases, select the database, and click Delete. In phpMyAdmin (a web-based MySQL tool), select the database from the left sidebar, click the Operations tab, and choose Drop Database.
The steps vary by provider, but all of them require you to confirm the deletion before it happens. Use the control panel method if your hosting account does not allow command-line access or if DROP DATABASE is blocked.
What happens when you delete a database
When you run DROP DATABASE, MySQL removes the database directory and all files inside it — tables, indexes, stored procedures, triggers, and views all disappear. Any application or script that tries to connect to that database will fail with a "database does not exist" error.
If other users or applications are currently using the database, MySQL will still delete it. The connections will break, and any queries in progress will fail. For this reason, it is good practice to stop applications that use the database before you delete it, or to delete it during a maintenance window when no one is using it.
How to back up a database before deleting it
Before you delete a database, consider backing it up in case you need the data later. The simplest way is to use mysqldump, a command-line tool that exports a database to a file. Type mysqldump -u username -p database_name > backup.sql and press Enter, then enter your password. This creates a file called backup.sql containing all the database structure and data.
You can restore the database later by running mysql -u username -p database_name < backup.sql. This approach takes a few seconds for small databases and longer for large ones, but it ensures you have a copy of the data before you delete it.
Frequently Asked Questions
Can I undo a DROP DATABASE command?
No. Once you run DROP DATABASE, the database is permanently deleted and cannot be recovered through MySQL. Your only option is to restore from a backup file created with mysqldump or another backup tool. This is why it is critical to verify the database name before you execute the command.
What is the difference between DROP DATABASE and DELETE?
DROP DATABASE removes the entire database and all its contents. DELETE removes individual rows from a table. If you want to keep the database but remove all data from a table, use DELETE or TRUNCATE TABLE instead of DROP DATABASE.
Do I need to close connections before dropping a database?
MySQL will drop the database even if applications are connected to it, but the connections will break and queries will fail. It is safer to stop applications using the database first, or to drop it during a scheduled maintenance window when no one is accessing it.
Why does DROP DATABASE fail with a permission error?
You do not have the DROP privilege on that database. Contact your database administrator or hosting provider to grant you the privilege, or use an account that already has it. Some hosting providers disable DROP DATABASE entirely for security and require you to delete databases through their control panel instead.
Can I delete a database if I do not know its exact name?
Run SHOW DATABASES; to see a list of all databases on the server. Find the one you want to delete, verify the exact spelling and capitalization, and then run DROP DATABASE with that name. This prevents you from accidentally deleting the wrong database.