How to Delete Everything in a MongoDB Database?
Last Updated :
18 Jul, 2024
In MongoDB, Deleting everything in the database is the most preventive task. These operations are irreversible, and all data, collections, indexes, and even the database itself will be permanently deleted.
In this article, we'll explore different approaches to deleting all data in a MongoDB database from deleting individual collections to dropping the entire database.
How to Delete Everything in MongoDB Database
When it comes to MongoDB, deleting all data can be solved in several ways. Below are the approaches that can help us to delete everything in different ways as follows:
- Using dropDatabase()
- Using deleteMany({})
1. Using dropDatabase()
In MongoDB, dropDatabase method is used to drop the currently selected database, it can delete all collections, indexed, and data. If you use this, you can't reverse the data Make sure you have backed up your data before dropping a database.
Syntax:
db.dropDatabase()
- db: current database connection.
- dropDatabase(): method that drops (deletes) the currently selected database.
Example: Delete the Entire GFG Database
Query:
use gfg
db.dropDatabase()
Output:

The above query initially switches to the database named called "gfg" using the use command, and it executes the db.dropDatabase() method to drop (delete) the entire "gfg" database, including all its collections, indexes, and data.
3. Using deleteMany()
In the MongoDB, deleteMany method is used to delete multiple documents from the collection that match the given filter criteria. If you use this, you can't reverse the data deleteMany method permanently deletes the documents from the collection.
Syntax:
db.collection.deleteMany(filter)
- db: The database object.
- collection: The name of the collection from which you want to delete documents.
- filter: A filter object that specifies the documents to delete.
Example: Delete the Tithe Field in Courses in Collection
Query:
use gfg
db.courses.deleteMany({ Title: "Java" })
Output:
1.png)
The above query initially switches to the "gfg" database using the use command, and then executes the db.courses.deleteMany({ Title: "Java" }) method to delete all documents from the "courses" collection where the "Title" field has the value "Java".
Conclusion
Overall summary, the drop method is used to delete only collections. To drop the entire database you can use dropDatabase method. and deleteMany method does not automatically drop the collection. When you truly want to delete and remove the entire database and start new database then use the above methods, it cannot be can't reverse the data.
Similar Reads
How to Rename a MongoDB Database? Renaming a MongoDB database may be necessary for various reasons such as reorganization or clarity. MongoDB provides several methods for renaming a database including using shell commands, dumping and restoring data or directly manipulating database files.In this article, we will learn different app
4 min read
How to Delete a Field From a Document in Mongoose? Databases are an essential component of web development. They are used for storing and managing data. MongoDB is a widely used NoSQL database. A Mongoose is a tool designed to simplify interactions with MongoDB databases in Node.js applications, making it an elegant MongoDB object modeling solution.
5 min read
How to Create a MongoDB Dump of Database MongoDB is a popular NoSQL database known for its flexibility, scalability, and ease of use. However, to protect our data from potential data loss or corruption, itâs critical to have a reliable MongoDB backup strategy in place. In this article, we will go through the process of creating a MongoDB d
7 min read
MongoDB | Delete Database using MongoShell MongoDB is a NoSQL database system that uses dynamic schemas, making it highly flexible for developers. A MongoDB database acts as a container for collections, where each collection contains documents. In this article, we will explain how to delete databases in MongoDB using the db.dropDatabase() co
4 min read
How to drop all databases present in MongoDb using Node.js ? MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read