How to remove databases with MongoDB Drop Database

You can use the MongoDB Drop Database command to remove a selected database. To get an overview of your databases both before and after removal you can use the MongoDB command show dbs.

What is the MongoDB drop database command?

Unlike database management systems such as MySQL, which follow a relational concept, MongoDB aims to be completely scalable and flexible. There is a wide range of MongoDB commands available. Data is saved, collated in collections using MongoDB Create Collection and indexed automatically or manually by using MongoDB Create Index. On top of this, you can use MongoDB Create Database to create databases and subsequently manage them. But what happens if a database is old or no longer needed? You can use MongoDB Drop Database to remove it.

What is the MongoDB Drop Database syntax?

The syntax for the command is very short and is as follows:

db.dropDatabase()

You do not need to write the name of the database to be deleted explicitly in the command. This isn’t needed because the command is carried out within the database.

How to use MongoDB Drop Database

If you want to use MongoDB Drop Database to remove a database, you need to follow four steps. Let’s say you want to remove a database that you no longer need called “client list”. You can do this as follows:

Pull up your databases

The first step is to check which databases you have. This allows you to avoid any errors and gives you a good overview. You can do this with the command show dbs:

>show dbs
admin 0.7278GB
local 0.5388GB
client list 0.6636GB
test 0.7624GB
>

By using the command, you can see that there is a database called “client list”.

Open the correct database

Before using MongoDB Drop Database, it’s important that you switch to the correct database first, i.e., the one which you want to delete. The system will confirm that you are in the correct database. You can do this by using the command use:

>use client list
switched to db client list
>

The system will confirm that you are in the correct database. Now you are in, you can carry out the MongoDB Drop Database command risk free. If you haven’t selected a database, the system will remove a test database. This isn’t a problem, but it also doesn’t offer you anything.

Removal using MongoDB Drop Database

If you use drop database to remove a database, the system will confirm that it has been successful. It will look something like this:

>use client list
switched to db client list
>db.dropDatabase()
>{ "dropped" : "client list", "ok" : 1 }
>

Checking the removal

Double check your database directory again to be certain that the MongoDB Drop Database command was successful using the command show dbs. If it was successful, your directory will look as follows:

>show dbs
admin 0.7278GB
local 0.5388GB
test 0.7624GB
>

Instead of the four databases shown previously, you have three remaining., meaning the removal was successful.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.