Easy Guide to Delete Everything in a MongoDB Database

In this tutorial, I will be discussing how to delete everything in a MongoDB database. Just like other developers, you too must have crammed your MongoDB database with some random data for testing software.

While testing or fidgeting with the database is usually done on the local MongoDB database, there are times when developers need to delete data living on the live database server.

Let us look at the different ways we can delete everything in a MongoDB database. This piece for beginners will cover an overview of what MongoDB is and how we can delete everything in a MongoDB database.

What is MongoDB?

MongoDB is a document-oriented database management system and allows developers to store mammoth amounts of data. It is open-source and very efficient. It is a NoSQL type database that is it does not store data inside tables but as documents inside collections.

MongoDB Inc. is the brain behind the MongoDB database. It has developed the database system and currently manages it. It was first launched in February 2009 and is managed under SSPL (Server Side Public License).

The database management system has gained immense popularity because of its driver support for most of the popular languages such as Nodejs, PHP, Java, Go, .Net, C, C++, Python, Ruby Scala, C#, Perl, Swift, Motor, and Mongoid. It backs as a database management system for applications built on these languages.

Companies like Facebook, Google, Adobe, Nokia, and many other top companies have chosen MongoDB to store their huge amounts of data.

Let us now get started on learning how to delete everything in a MongoDB database.

How to Delete Everything in a MongoDB Database

Let us get started with exploring the delete operation from the CRUD operations of MongoDB. To delete everything in a MongoDB database such as the documents in a collection, a collection inside of a database, or the database itself. Let us get started.

Follow the steps enlisted below to perform the delete operations on a MongoDB database using the mongo shell.

1. Deleting Documents Inside a Collection

  1. Start your MongoDB database server. If you don’t have the .bat file to start the MongoDB server, open a new file in VS Code. Paste this code:
@echo off
cd "C:\mongodb\bin"
start mongod.exe
timeout 4
start mongo.exe
exit

Now, save the file on the desired location as a Windows Batch file (.bat). I have named mine StartMongo.bat.

  • Now, start your MongoDB database server by running the bat file if you haven’t. You should now have the mongo shell open automatically for you.
  • To view a list of all your databases, pass this command:
show dbs
  • You should now see a list of databases available in your database. Let us suppose we found a database named countrymade-agro.
  • Now, move into the database like this:
use countrymade-agro
  • In it we have the reginasRanch collection. To view all collections, pass this command:
show collections
  • To be able to use this collection, you need to move into it. Pass the command to do it:
use reginasRanch

You are now inside the reginasRanch collection. Let’s say we have some data stored here about the animals we breed on the ranch; in the form of documents. Now, we want to delete some documents out of this collection may be because some of our animals died, got sold, or for some other reason.

Deleting documents can be done in different ways. We might want to delete just one document or may want to delete many, inside a collection. If we want, we can pass a condition to MongoDB and let it know what it should look for in the document(s) to be deleted.

Once a condition is matched to a few documents, we might want to delete all of them. Or, we may just want to just delete one of them. Otherwise, we just might want to clear out our collection altogether and start afresh.

I will dissect the document deleting operations based on how we want to delete them.

  1. To delete all documents inside a collection:
db.reginasRanch.deleteMany({})
  • To delete all documents inside a collection that match a given condition:
db.reginasRanch.deleteMany( { animal: "Horse" } )
  • To delete a single document that matches a given condition:
db.reginasRanch.deleteOne( { name: "Blue" } )

When we leave the curly braces blank without a condition, MongoDB understands that we want to target everything inside the collection. Please be extra careful while using the delete operation with blank curly braces.

Deleting a Collection Inside a Database

Let’s say Regina’s Ranch is closing and we need to delete its collection. To delete an entire collection inside a database, follow the steps mentioned below:

  1. Start your MongoDB server.
  2. To delete an entire collection, pass this command in the mongo shell:
db.reginasRanch.drop()

Deleting an Entire Database

Well, let’s say CountryMade Agro itself is shutting down. Now, we need to delete the entire countrymade-agro database. Follow the steps below to do so:

  1. Start your MongoDB server.
  2. Move into the countrymade-agro database like this:
use countrymade-agro
  • Now, simply delete or drop it by using this command:
db.dropDatabase()

Read more: Migrating WordPress Content to MongoDB

Conclusion

This article explains how to delete everything in a MongoDB database.

Noteworthy References

Answer on StackOverflow

MongoDB Official Docs

Aneesha S
Aneesha S
Articles: 174