How to delete databases permanently with SQL DATABASE

SQL DROP DATABASE is used to permanently delete an entire database including all tables and entries. The SQL command should therefore only be used if the database is actually no longer required or corresponding backups exist. It is also possible to remove several databases at the same time.

What is SQL DROP DATABASE?

When working with Structured Query Language, your initial steps typically involve creating a new database using SQL CREATE DATABASE. Next, create one or more tables with SQL CREATE TABLE, which you can then fill with data. To prevent obsolete databases from occupying storage space or potentially leaving sensitive data unprotected, it’s important to delete them when they are no longer needed. For this, SQL provides the SQL DROP DATABASE command, which removes the entire database and all its connections.

It is important that you take the necessary precautions in advance. Once a database has been deleted with this command, all data is irretrievably lost. Therefore, make sure that you create backups using the SQL BACKUP DATABASE command. You should also make sure that no important programs or processes rely on the database. The deletion process typically can only be executed by an administrator. Therefore, if you want to use the SQL DROP DATABASE command, you’ll need the necessary permissions.

VPS Hosting
Fully virtualised servers with root access
  • Unlimited traffic and up to 1 Gbit/s bandwidth
  • Fast SSD NVMe storage
  • Free Plesk Web Host Edition

Syntax and function

The syntax of SQL DROP DATABASE is as follows:

DROP DATABASE name_of_database;
sql

You only need to insert the name of the corresponding database and execute the code. This will remove the database, along with all its tables and entries, making it inaccessible.

Example of the deletion process

In practice, SQL DROP DATABASE command might look like this. Suppose we have a database called ‘Customers’, but its entries are outdated. Since we’ve already created a new database with the necessary data, we want to remove the entire old database rather than deleting each table individually. The command would be:

DROP DATABASE Customers;
sql

Combination with IF EXISTS

If you’re unsure whether a particular database exists, you can use SQL DROP DATABASE with the IF EXISTS command. This command first checks whether a database with the corresponding name exists. If it does, the database is deleted; if not, the command simply does nothing, avoiding an error message. For our example above, the command would be:

DROP DATABASE IF EXISTS Customers;
sql

Delete multiple databases

To delete several databases at the same time, simply list them with commas. SQL DROP DATABASE could then look like this:

DROP DATABASE Customers, Employees, Branches;
sql

In this case, you would remove the Customers, Employees and Branches databases at the same time.

Display existing databases

To check which databases are on your system, use the SHOW DATABASE command. This will list all databases. If the deletion was successful, the removed databases will no longer appear in the list. This command is an effective method for verifying that the SQL DROP DATABASE command was executed correctly. The syntax is as follows:

SHOW DATABASES;
sql

Similar commands to SQL DROP DATABASE

In many cases, you may not want to delete the entire database but only individual entries or tables. SQL offers several commands for this purpose. To remove an entire table, use DROP TABLE. If you want to keep the table but empty it completely, TRUNCATE TABLE is the right choice. You can also use the SQL DELETE statement can also be used to empty a table entirely, and it allows for conditions so that only specific columns or entries are affected if needed.

Tip

MSSQL, MySQL or MariaDB? With SQL Server Hosting from IONOS, you get to choose and benefit from strong performance, extensive security features and expert, personal advice.

Was this article helpful?
Page top