To drop all databases in MongoDB, you can use the following command:
1 2 3 4 5 6 |
use admin db.adminCommand({ listDatabases: 1 }).databases.forEach(function (d) { if (d.name !== 'admin' && d.name !== 'local' && d.name !== 'config') { db.getSiblingDB(d.name).dropDatabase(); } }); |
This script will first switch to the admin database, fetch a list of databases using the listDatabases
command, and then iterate through each database apart from the built-in ones like admin
, local
, and config
to drop them one by one. By running this script, you can effectively drop all user-created databases in MongoDB.
How to drop all databases in MongoDB using a GUI tool?
To drop all databases in MongoDB using a GUI tool, you can use a tool like MongoDB Compass. Here's how you can do it:
- Open MongoDB Compass and connect to your MongoDB server.
- Once connected, click on the "Databases" tab on the left-hand side to view all the databases in your MongoDB server.
- Right-click on each database one by one and select the "Drop Database" option. Confirm the action when prompted.
- Repeat this process for each database until all databases have been dropped.
Please note that dropping all databases will permanently delete all data in your MongoDB server, so be sure to back up any important data before proceeding with this action.
How to drop all databases in MongoDB with confirmation prompt?
To drop all databases in MongoDB with a confirmation prompt, you can use the following steps:
- Connect to your MongoDB instance using the mongo shell or a GUI tool such as MongoDB Compass.
- Run the following command to list all the databases:
1
|
show dbs
|
- Make sure you are in the correct database and then run the following command to drop all databases:
1 2 3 4 5 6 |
db.getMongo().getDBNames().forEach(function(database){ if(database != 'admin' && database != 'local' && database != 'config'){ print('Dropping database: ' + database); db.getSiblingDB(database).dropDatabase(); } }); |
- This command will drop all databases except for the system databases like 'admin', 'local', and 'config'.
- The command will prompt you for confirmation before dropping each database. Make sure to carefully review the output and confirm the drop for each database.
- Once you have confirmed the drop for each database, all databases except for the system databases will be dropped.
Please make sure to backup any important data before dropping all databases, as this operation cannot be undone.
How to drop all databases in MongoDB using Studio 3T?
To drop all databases in MongoDB using Studio 3T, follow these steps:
- Connect to your MongoDB server in Studio 3T.
- In the Connection Tree on the left-hand side, right-click on the server connection and choose the "Open Shell" option.
- In the Shell tab that appears, run the following command to list all databases:
1
|
show dbs
|
- Identify the databases you want to drop and note down their names.
- Now, run the following command to drop a database in MongoDB:
1 2 |
use <databaseName> db.dropDatabase() |
Replace <databaseName>
with the name of the database you want to drop. Repeat this command for each database you want to drop.
6. After dropping all the databases you want to delete, run the "show dbs" command once more to verify that all databases have been dropped.
Please note that dropping databases is a destructive operation and cannot be undone. Make sure to backup any important data before proceeding with these steps.