Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to delete everything in a MongoDB database?
You can delete everything in a MongoDB database using the dropDatabase() method. This operation permanently removes the entire database and all its collections.
Syntax
use databaseName; db.dropDatabase();
Example: Delete Entire Database
Let's first display all existing databases ?
show dbs
admin 0.000GB config 0.000GB flightInformation 0.000GB local 0.000GB sample 0.000GB sampleDemo 0.000GB test 0.003GB
Now we'll delete everything from the flightInformation database. First, switch to that database ?
use flightInformation
switched to db flightInformation
Execute the drop operation ?
db.dropDatabase()
{ "dropped" : "flightInformation", "ok" : 1 }
Verify Database Deletion
List databases again to confirm the deletion ?
show dbs
admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB sampleDemo 0.000GB test 0.003GB
The flightInformation database has been completely removed.
Key Points
-
dropDatabase()is irreversible ? all data, collections, and indexes are permanently deleted. - You must
usethe target database before callingdropDatabase(). - The operation returns a confirmation with the dropped database name.
Conclusion
Use db.dropDatabase() to completely remove a MongoDB database and all its contents. Always ensure you're in the correct database context before executing this destructive operation.
Advertisements
