View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

How to Drop a MongoDB Database?

By Rohit Sharma

Updated on Oct 27, 2024 | 6 min read | 8.7k views

Share:

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.

Prerequisites

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.

Overview

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. 

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.

background

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree18 Months
View Program

Placement Assistance

Certification8-8.5 Months
View Program

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.

Enhance your expertise with our Software Development Free Courses. Explore the programs below to find your perfect fit.

Elevate your expertise with our range of Popular Software Engineering Courses. Browse the programs below to discover your ideal fit.

Advance your in-demand software development skills with our top programs. Discover the right course for you below.

Explore popular articles related to software to enhance your knowledge. Browse the programs below to find your ideal match.

Conclusion

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.

Frequently Asked Questions (FAQs)

1. What is Sharding in databases?

2. What are the advantages of using an RDBMS over MongoDB?

3. What are the use cases of MongoDB?

Rohit Sharma

694 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in Data Science & AI

Placement Assistance

Executive PG Program

12 Months

View Program
Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree

18 Months

View Program
upGrad Logo

Certification

3 Months

View Program