MongoDB Delete Database
MongoDB Delete Database – In this MongoDB Tutorial, we shall learn to drop or delete a MongoDB Database.
In MongoDB, deleting a database means removing the selected database and its collections from the MongoDB server. The command used for this operation is db.dropDatabase(). Since this action removes the database data, use it only after confirming that you are connected to the correct MongoDB instance and database.
MongoDB drop database command syntax
The MongoDB shell command to delete the current database is:
db.dropDatabase()
The command runs against the database that is currently selected in the shell. Therefore, the common pattern is to first switch to the target database using use, and then run db.dropDatabase().
use database_name
db.dropDatabase()
To delete or drop a database from MongoDB, follow these steps
- Select the database you want to delete with the help of USE <database> command.
Following is the syntax of USE command.use <database_name> - Drop the database with the help of db.dropDatabase() command.
Following is the syntax of USE command.db.dropDatabase()
Before deleting a MongoDB database
Before running the MongoDB delete database command, check the database name carefully. If you are working on a local development machine, this may only affect test data. If you are connected to a shared, staging, or production server, the same command can remove data that other applications depend on.
- Run
show dbsto list available databases. - Use
dbafter switching databases to confirm the current database name. - Take a backup when the database contains data that may be needed later.
- Check whether your MongoDB user has the required permissions to drop a database.
- Do not run the command from an application shell or script unless the database name is controlled and verified.
Example – Drop MongoDB Database
Following is an example where we shall try deleting database named tutorialkart.
Refer MongoDB Create Database, if you have not already created one.
Open Mongo Shell and follow the commands in sequence.
> show dbs
admin 0.000GB
local 0.000GB
tutorialkart 0.000GB
> use tutorialkart
switched to db tutorialkart
> db.dropDatabase()
{ "dropped" : "tutorialkart", "ok" : 1 }
> show dbs
admin 0.000GB
local 0.000GB
>
Following is the explanation for each mongodb command we executed above
- show dbs there are three databases. We shall delete tutorialkart database in this demonstration.
- use tutorialkart switched to tutorialkart database.
- db.dropDatabase() drops the database that is currently in use i.e., tutorialkart database.
- show dbs now there are only two databases, because tutorialkart database is no more present.
Verify the selected MongoDB database before dropping it
You can also print the current database name before running db.dropDatabase(). This is useful when multiple terminal tabs or MongoDB connections are open.
use tutorialkart
db
The shell displays the currently selected database name.
tutorialkart
After confirming the name, run the drop command.
db.dropDatabase()
Delete all collections in a MongoDB database without dropping the database
Dropping a MongoDB database is different from deleting documents or removing collections. If you want to keep the database but delete data inside it, use collection-level commands instead of db.dropDatabase().
| Requirement | MongoDB command | What it removes |
|---|---|---|
| Delete one database | db.dropDatabase() | The current database and its collections |
| Delete one collection | db.collection.drop() | A single collection |
| Delete matching documents | db.collection.deleteMany(filter) | Documents that match the filter |
| Delete all documents in one collection | db.collection.deleteMany({}) | All documents in that collection, while keeping the collection |
For example, the following command removes every document from the customers collection but does not drop the database.
db.customers.deleteMany({})
Drop a MongoDB database from the command line with mongosh
If you want to run the delete database command directly from a terminal, you can start mongosh, connect to the MongoDB server, select the database, and execute the same command.
mongosh
use tutorialkart
db.dropDatabase()
For scripts and automation, avoid hardcoding production database names. Use environment-specific configuration and add checks before running destructive commands.
Common MongoDB delete database mistakes
- Running the command on the wrong database: Always confirm the selected database with
dbbefore dropping it. - Assuming
use database_namecreates permanent data: A MongoDB database may not appear inshow dbsuntil it has data. - Confusing document deletion with database deletion: Use
deleteMany()for documents anddropDatabase()only for the whole database. - Skipping backup for important data: Dropping a database is a destructive action.
- Testing against the wrong MongoDB server: Check the connection string or host before running the command.
QA checklist for this MongoDB delete database tutorial
- The tutorial explains that
db.dropDatabase()deletes the currently selected MongoDB database. - The example shows
show dbs,use tutorialkart,db.dropDatabase(), and the final database list. - The tutorial distinguishes dropping a database from deleting documents with
deleteMany(). - The safety notes remind readers to confirm the database name and backup important data.
- The command-line example uses
mongosh, which is the current MongoDB shell.
MongoDB delete database FAQs
How do I completely delete a MongoDB database?
Switch to the database using use database_name, confirm that the selected database is correct, and run db.dropDatabase(). The command deletes the current database.
How do I delete all databases in MongoDB?
MongoDB does not require you to delete all databases as a normal administration task. If you really need to remove multiple databases, list them with show dbs and drop each selected database separately. Do not drop system databases such as admin, config, or local unless you fully understand the impact.
Does db.dropDatabase() delete collections also?
Yes. db.dropDatabase() removes the current database, including its collections and data. If you want to remove only one collection, use db.collection.drop() instead.
Why is my MongoDB database not visible after using use database_name?
The use command switches to a database name, but MongoDB may not list that database in show dbs until data is inserted into it.
Can I undo a MongoDB drop database command?
You cannot undo db.dropDatabase() from the shell after it succeeds. Restore the database from a backup if you need the dropped data again.
Conclusion: deleting a MongoDB database safely
In this MongoDB Tutorial, we learned how to delete a MongoDB Database using db.dropDatabase(). The important point is that the command drops the database currently selected in the shell, so always verify the database name before running it.
TutorialKart.com