How to Drop a MongoDB Database?

Read it in 6 Mins

Last updated on
15th Jun, 2021
15th Jun, 2021
How to Drop a MongoDB Database?

MongoDB is the most popular NoSQL database among both enterprise and startups, and the fact that it is highly scalable makes it perfectly suited for current web-apps, which need to scale once the user base increases. MongoDB is different from traditional relational databases because it uses json like objects to store data, instead of tables in relational databases.

In this post, we will learn to drop a MongoDB database, using MongoDB Drop Database command. Dropping a database means dropping all the collections (tables in MongoDB) in it, along with the indexes. If you don’t want to drop the entire database, you can drop individual collections.


We are using Windows 10 in this tutorial. Please make sure you have downloaded the MongoDB Community Server and installed it. It is a very easy setup and you will find a lot of good articles on the internet to guide you through this process. Do make sure that you have added it in the Environment variable in your PC. We have also created an example database, which contains a collection named products.


Dropping a database is quite simple and can be done with three methods from the command line and also by any GUI (Graphical User Interface) tool, which is used to connect with the running MongoDB instance.

To delete a MongoDB database through the command line, we mainly use db.dropDatabase(). We will first understand how to do this.

Drop Database definition

As mentioned earlier, we drop a database in MongoDB, or delete MongoDB database using the db.dropDatabase() command.


This removes the current database, deleting all collections inside it, along with the indexes.

writeConcern is the optional parameter, which is a document expressing the write concern to use, if greater than majority. Write concern can include the following fields:

{ w: <value>, j: <boolean>, wtimeout: <number> }
  • w option is used to request acknowledgement, that the write operation has passed to a specified number of mongod instances.
  • j option is used to request acknowledgement, that the write operation had been written to the disk-journal
  • wtimeout option is used to specify the time limit, to prevent the write operations from blocking indefinitely.

Using the w option, the following <value> are available:

  • w: 1 – Requests acknowledgement that the write concern has passed to the single mongod instance. It is also the default option.
  • w: 0 – Requests no acknowledgement of the write operation.
  • w: “majority” – It requests acknowledgement that the write operation has passed to the majority of primary and secondary servers.

Now, we will look into the j option values.

  • j: true – It requests acknowledgement that the different mongod instances, mentioned in w: <value>, have been written to the on-disk journal.

The wtimeout option specifies the time limit in milliseconds, for the write concern. It causes write operations to return with an error after the specified limit.


We can also use the dropDatabase command directly to delete MongoDB, current database. The command has the following form.

{ dropDatabase: 1, writeConcern: <document>, comment: <any> }

This command optional fields are-

  • writeConcern is the optional parameter, which is in the form of a document expressing the write concern to use if it is greater than majority. We have just looked into it in detail earlier.
  • comment is a user provided comment, which is attached to this command. Once set, this will appear in the logs.

Dropping with dropDatabase Command

We can drop the database through dropDatabase command. I am connected to my local mongoDB instance from the windows terminal, by giving the mongo command.

> mongo

After that to show all the databases in my MongoDB database, we will use the command show dbs command. It will show all our databases. Out of it I created the example database only.

> show dbs
admin    0.000GB
config   0.000GB
example  0.000GB
local    0.000GB

Now, to drop the example database, first we have to go inside it by using the use example command.

> use example
switched to db example

Now, we will use the dropDatabase command from within a db.runCommand(). Now, the runCommand runs the command in the context of the current database. This means it will run the dropDatabase command in the current database, which is seen in this example:

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

As discussed, the earlier dropDatabase command accepts an optional writeConcern argument, and also a comment field. The syntax then is written as shown below.

{ dropDatabase: 1, writeConcern: <document>, comment: <any> }

Dropping with dropDatabase() Method

We can drop the database through db.dropDatabase() method also. It also removes the current database, deleting all associated files.

Once again connect to mongoDb database by giving the command mongo. After that we can check all the databases with show dbs command.

For db.dropDatabase() method also, we need to go inside the database that we are dropping, by using use <dbName> command.

Now, we will use the db.dropDatabase() command to drop the database, which is example in our case.

> db.dropDatabase()
{ "dropped" : "example", "ok" : 1 }
The db.dropDatabase() method also accepts an optional writeConcern argument. The syntax then becomes:
{ w: <value>, j: <boolean>, wtimeout: <number> }

Dropping with Unix Shell and Eval command

This method only works in a Unix environment like Ubuntu, Mac or WSL2 for Windows. Here, the mongo command with the database name can be used to connect to a specific database. For example, to connect to the example database, we can use the below command.

$ mongo example

Now, we can drop our example database with one line command, where we use the eval command followed by the JavaScript code that we wish MongoDB to execute. As seen earlier to drop the database, we use the db.dropDatabase() command. Now, we will wrap this method in printjson function, to get the output of the command.

$ mongo example --eval "printjson(db.dropDatabase())"
MongoDB shell version: 3.0.9
connecting to: example
{ "dropped" : "example", "ok" : 1 }

Dropping with MongoDB Compass

Now we can drop a database or delete MongoDB database very easily using a Graphical tool like MongoDB compass. Infact this tool was auto-downloaded when I had installed MongoDB on my Windows machine.

So, first open the MongoDB Compass and you will see the option to connect to a database. We want to connect to our local database, in which the hostname is localhost and port is 27017. These are the default values for all MongoDB connections, unless you changed it during installation.

Don’t change anything and click on the Connect button.

We are now connected to the MongoDB running on our localhost and we can see all the databases including example database. Next, hover over example database, which will show a delete button. Click on the delete button.

We will be shown a pop-up where we have to write the name of our database, which is example in our case. Then click on the DROP DATABASE button, to MongoDB remove database.

After that it will take us back to the earlier screen and we can see the example database is dropped.


In this article, we have learned about the different MongoDB delete database commands. We have also learned about the write concern optional parameter, which can be passed to both db.dropDatabase() method and dropDatabase command. This parameter is mainly used in large database systems, which have several servers. 

We hope you found this article useful! Keep learning.


Nabendu Biswas


Nabendu Biswas is a Full Stack JavaScript Developer, who has been working in the IT industry for the past 16 years and has worked for world’s top development firms, and Investment banks. He is a passionate tech blogger. He is also a tech youtuber and loves to teach people JavaScript. He is also an Apress author with three Gatsby books published.