MongoDB Delete Database
Deleting a database in MongoDB is simple but permanent, so proceed with caution. This guide covers how to delete a MongoDB database using MongoDB Shell, Node.js (Mongoose), and MongoDB Compass.
1. Delete a Database Using MongoDB Shell
A. Open MongoDB Shell
Start the MongoDB shell:
B. Select the Database to Delete
C. Drop the Database
✅ Example Output:
This confirms that blogDB
has been deleted.
D. Verify the Deletion
✅ Expected Output:
If blogDB
is missing, it was successfully deleted.
2. Delete a Database Using Node.js (Mongoose)
A. Install Mongoose (If Not Installed)
B. Create a Script to Delete the Database
Create deleteDatabase.js
:
C. Run the Script
✅ Expected Output:
3. Delete a Database Using MongoDB Compass
If you prefer a graphical interface, follow these steps:
- Open MongoDB Compass and connect to your server.
- Locate the database name (e.g.,
blogDB
). - Click on the trash icon or right-click → Drop Database.
- Confirm the deletion in the pop-up window.
✅ The database will be permanently removed.
4. Summary
✔ Deleted a database using MongoDB Shell (dropDatabase()
).
✔ Used Mongoose to drop a database programmatically.
✔ Removed a database using MongoDB Compass GUI.
5. Next Steps
🔹 Schedule automatic backups before deleting databases.
🔹 Use authentication to prevent accidental deletions.
🔹 Monitor database size and optimize storage.
Would you like help setting up database backups? 🚀