Last Updated : 11 Jul, 2025
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()
command in MongoShell, with examples and step-by-step instructions.
To effectively manage and delete databases in MongoDB, it’s essential to understand its hierarchical structure. A database acts as a container for collections, while collections store documents, which are the fundamental units of data in MongoDB. This dynamic schema design offers flexibility for varied data storage needs.
What is a Database?A MongoDB database is a container for all collections. Collections are similar to tables in relational databases, and they store documents. MongoDB allows you to manage multiple databases on a single server, providing flexibility and scalability for diverse applications.
What is a Collection?A Collection is a group of MongoDB documents, analogous to tables in relational databases. However, collections do not enforce a fixed schema, enabling dynamic storage of data. Each document within a collection can have different fields.
Example of a Collection:[What is a Document?
{
"Name": "Aman",
"Age": 24,
"Gender": "Male"
},
{
"Name": "Suraj",
"Age": 32,
"Gender": "Male"
},
{
"Name": "Joyita",
"Age": 21,
"Gender": "Female"
}
]
A document is a basic unit of data in MongoDB. It consists of field-value pairs and can vary in structure within the same collection. This flexibility is due to MongoDB’s dynamic schema design.
Example of a Document :
{What is MongoShell?
"Name" : "Aman",
Age : 24,
Gender : "Male"
}
The MongoShell is an interactive JavaScript interface used to query, update data, and perform administrative operations in MongoDB. It serves as a command-line tool for managing databases and collections, making it an essential resource for developers.
How to Delete a Database in MongoDB Using MongoShellDeleting a database in MongoDB is a straightforward process using the db.dropDatabase()
command. This command permanently removes the currently selected database along with all its collections and documents.
Syntax :
db.dropDatabase()
This command is used to drop an existing database. This command will delete the currently selected database. If you have not selected any database, then it will delete the default 'test' database.
Example: Deleting a DatabaseTo delete the userDB
database, first select it using the use userDB
command. Then, execute the db.dropDatabase()
command to permanently remove the database along with all its collections and documents.
Query:
// Step 1: Select the database to delete
use userDB// Step 2: Drop the selected database
db.dropDatabase()
Output:
This confirms that the database userDB
has been deleted.
Sometimes, you may want to delete a database without selecting it. Follow these steps:
1. Check the Current Database:
db
This command returns the name of the currently selected database.
2. List All Databases:
show dbs
This command lists all the available databases on the MongoDB server.
3. Select the Database to Delete:
use adminDB
Replace adminDB
with the name of the database you want to delete.
4. Delete the Selected Database:
db.dropDatabase()Example:
Let’s assume the currently selected database is userDB
, but you want to delete adminDB
instead:
Output:
{
"ok": 1,
"dropped": "adminDB"
}
This confirms that the adminDB
database has been deleted successfully.
db.dropDatabase()
command deletes the default test
database.The db.dropDatabase()
command in MongoShell provides a simple and efficient way to delete databases in MongoDB. Whether you want to delete the currently selected database or a different one, following the steps outlined in this guide ensures the operation is performed correctly. By understanding MongoDB’s structure and using the dropDatabase
command effectively, you can manage your databases with confidence and precision
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4