How to Delete a MySQL Database on Linux via Command Line

By 

Updated on

4 min read

How to Delete / Drop a MySQL Database

Sometimes you need to remove a MySQL or MariaDB database after a migration, a test cleanup, or when an application is no longer in use. In these cases, you use the DROP DATABASE command, which permanently removes the database and all of its tables and data. Because this action is irreversible, it is important to verify the database name and have a backup before running it.

This guide explains how to delete a MySQL database from the MySQL shell and from the command line using mysqladmin.

Before You Begin

We assume you already have MySQL or MariaDB installed on your system. This guide applies to MySQL 8.0 and later and MariaDB 10.5 and later.

To delete a database, your MySQL user account must have the DROP privilege on that database. All examples below use the root account.

To open the MySQL shell, run the following command and enter your password when prompted:

Terminal
mysql -u root -p
Info
If you have not set a password for your MySQL root user, you can omit the -p option.

Syntax

The general syntax of the DROP DATABASE command is:

Terminal
DROP DATABASE [IF EXISTS] database_name;
  • database_name — The name of the database to delete. On Linux, database names are case-sensitive.
  • IF EXISTS — Optional. Prevents an error if the database does not exist.

Deleting a Database

Before dropping a database, you may want to list all databases on the server to confirm the name:

Terminal
SHOW DATABASES;
Info
It is always a good idea to create a backup before dropping a database.

To delete a database, run the DROP DATABASE command followed by the database name:

Terminal
DROP DATABASE my_database;
output
Query OK, 1 row affected (0.00 sec)

If the database does not exist, MySQL returns an error:

output
ERROR 1008 (HY000): Can't drop database 'my_database'; database doesn't exist

To suppress this error, use IF EXISTS:

Terminal
DROP DATABASE IF EXISTS my_database;
output
Query OK, 0 rows affected, 1 warning (0.00 sec)

The 1 warning indicates that the database did not exist, so nothing was deleted.

Deleting a Database with mysqladmin

You can also delete a database directly from the terminal using the mysqladmin utility, without opening the MySQL shell:

Terminal
mysqladmin -u root -p drop my_database

The command will ask you to confirm the action before proceeding.

Quick Reference

CommandDescription
DROP DATABASE db_name;Delete a database
DROP DATABASE IF EXISTS db_name;Delete a database only if it exists
SHOW DATABASES;List all databases on the server
mysqladmin -u root -p drop db_nameDelete a database from the terminal

Troubleshooting

ERROR 1008: Can not drop database; database does not exist
The database name is misspelled or the database has already been deleted. Run SHOW DATABASES; to verify the exact name. Remember that database names are case-sensitive on Linux.

ERROR 1044: Access denied for user
Your user account does not have the DROP privilege on that database. Log in as root or ask an administrator to grant the privilege.

Accidentally dropped the wrong database
There is no undo for DROP DATABASE. If you have a backup created with mysqldump , you can restore it. Otherwise, the data is permanently lost.

FAQ

Does DROP DATABASE delete all tables inside it?
Yes. The DROP DATABASE command removes the database and all of its tables, views, indexes, and stored procedures.

Can I drop a database while other users are connected to it?
Yes. MySQL allows you to drop a database even if there are active connections using it. Those connections will receive errors on their next query.

Is DROP DATABASE the same as DROP SCHEMA?
Yes. DROP SCHEMA is a synonym for DROP DATABASE in MySQL and MariaDB.

Conclusion

The DROP DATABASE command permanently deletes a MySQL database and all of its contents. Always verify the database name and create a backup before dropping. For related tasks, see our guides on creating a database and managing databases and users .

If you have any questions, feel free to leave a comment below.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page