How to Delete a SQL Database?

To delete an existing SQL database, you can use the DROP DATABASE statement like so:

DROP DATABASE foo;

The above statement would drop the database called "foo". The DROP DATABASE statement is pretty much synonymous across all SQL databases, with the exception of maybe embedded SQL databases like SQLite — where it doesn't make much sense as the entire database is contained in a single file. For such embedded databases, simply deleting the database file would suffice.

Please keep in mind though that most SQL databases (such as MySQL, MariaDB, SQL Server, PostgreSQL, etc.) would throw an error if you attempt to drop a non-existent database. To counter that, most SQL databases support IF EXISTS which prevents an error from occurring if the database does not exist. Therefore, if IF EXISTS is supported in the SQL database you're using, it might be a good idea to drop the database in the following way:

DROP DATABASE IF EXISTS foo;

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.