MongoDB Drop Database

In this post, We will learn How to delete the existing mongo database.

MongoDB shell command `db.dropDatabase()` is used to drop an existing database and their associated data files. It will block other operations While dropping a database.

The basic syntax for deleting a database is

Syntax:

db.dropDatabase()

This command will drop the currently selected database. If we have not selected any database, It will drop the default database(test).

Database List:

If you want to get the list of available databases, use the below command.

> show dbs
DBNAME          0.000GB
admin           0.000GB
learnmongodb    0.000GB
local           0.000GB
test            0.000GB
>

Drop Database:

First we need to select a database Which one we like to remove.

>use learnmongodb
switched to db learnmongodb
>

Now If you execute `dropDatabase()` command, It will drop `learnmongodb` database.


>db.dropDatabase()
>{ "dropped" : "learnmongodb", "ok" : 1 }
>

Now check your database list and your selected database has been removed.

> show dbs
DBNAME          0.000GB
admin           0.000GB
local           0.000GB
test            0.000GB
>

Complete shell command:

> show dbs
DBNAME          0.000GB
admin           0.000GB
learnmongodb    0.000GB
local           0.000GB
test            0.000GB
>use learnmongodb
switched to db learnmongodb
>db.dropDatabase()
>{ "dropped" : "learnmongodb", "ok" : 1 }
> show dbs
DBNAME          0.000GB
admin           0.000GB
local           0.000GB
test            0.000GB
>
Note:

If you delete a mongo database and create a new mongo database with the same name, You have to restart all mongo instances or need to `flushRouterConfig` command for all instances. We have to do this before reading or writing from the database.