How to Drop a Database in MongoDB

Knowing how to drop a Database in MongoDB is essential for every developer. This tutorial shows you how to drop specified databases from the MongoDB server using different methods.

Drop Database in MongoDB

You can drop a database in MongoDB in two ways: first, using the MongoDB shell and second, using the MongoDB compass, a graphical user interface.

Drop Database in MongoDB Using Mongosh Shell

To drop the database, use the db.dropDatabase() command and open the Mongo shell by entering the “Mongosh” command.

mongosh

View all the databases.

show dbs

Then, switch to the database you want to drop; in my case, I will delete the database “library.”

use library

To drop the database, use the command below, which deletes the currently selected database.

db.dropDatabase()
Drop Database in MongoDB Using Mongosh Shell

After executing db.dropDatabase(), view all the databases again. You won’t see the “library” database because it has been deleted.

Drop Database in MongoDB Using Compass

The MongoDB compass is a graphical user interface for managing and manipulating data; you can use it to drop any database.

Open the MongoDB compass and go to the right section to view the entire database. Then, hover over the database you want to drop; a delete icon appears when you hover. To drop the database, click the delete icon in the picture below.

Drop Database in MongoDB Using Compass

As soon as you click on the delete icon, a dialogue appears. In it, you need to confirm the deletion of the database by typing its name in the field. So, type the database name you want to drop, then click the Drop Database button.

Drop Database in MongoDB Using Compass By Confirming Database Name

After clicking the Drop Database button, the database named ‘library’ is dropped successfully; you won’t see it in the database section at the right side of your window.

Conclusion

Dropping the database base is usually done whenever it is no longer required; knowing how to drop a database in MongoDB is a must, so you learned how to drop a database using the MongoDB shell and Compass.

You may also like reading the following MongoDB tutorials.