![]() |
VOOZH | about |
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.
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:
In MongoDB, dropDatabase methodis 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()Query:
use gfg
db.dropDatabase()
Output:
👁 Delete-the-Entire-GFG-Database
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.
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)Query:
use gfg
db.courses.deleteMany({ Title: "Java" })
Output:
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".
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.