How to use dropDatabase() In MongoDB
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.
How to Delete Everything in a MongoDB Database?
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.