How to Drop All Databases In Mongodb?

8 minutes read

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.

Best Database Books to Read in December 2024

1
Database Systems: The Complete Book

Rating is 5 out of 5

Database Systems: The Complete Book

2
Database Systems: Design, Implementation, & Management

Rating is 4.9 out of 5

Database Systems: Design, Implementation, & Management

3
Database Design for Mere Mortals: 25th Anniversary Edition

Rating is 4.8 out of 5

Database Design for Mere Mortals: 25th Anniversary Edition

4
Database Internals: A Deep Dive into How Distributed Data Systems Work

Rating is 4.7 out of 5

Database Internals: A Deep Dive into How Distributed Data Systems Work

5
Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems

Rating is 4.6 out of 5

Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems

6
Fundamentals of Data Engineering: Plan and Build Robust Data Systems

Rating is 4.5 out of 5

Fundamentals of Data Engineering: Plan and Build Robust Data Systems

7
Seven Databases in Seven Weeks: A Guide to Modern Databases and the NoSQL Movement

Rating is 4.4 out of 5

Seven Databases in Seven Weeks: A Guide to Modern Databases and the NoSQL Movement

8
SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL (Coding & Programming - QuickStart Guides)

Rating is 4.3 out of 5

SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL (Coding & Programming - QuickStart Guides)


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:

  1. Open MongoDB Compass and connect to your MongoDB server.
  2. Once connected, click on the "Databases" tab on the left-hand side to view all the databases in your MongoDB server.
  3. Right-click on each database one by one and select the "Drop Database" option. Confirm the action when prompted.
  4. 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:

  1. Connect to your MongoDB instance using the mongo shell or a GUI tool such as MongoDB Compass.
  2. Run the following command to list all the databases:
1
show dbs


  1. 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();
    }
});


  1. This command will drop all databases except for the system databases like 'admin', 'local', and 'config'.
  2. 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.
  3. 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:

  1. Connect to your MongoDB server in Studio 3T.
  2. In the Connection Tree on the left-hand side, right-click on the server connection and choose the "Open Shell" option.
  3. In the Shell tab that appears, run the following command to list all databases:
1
show dbs


  1. Identify the databases you want to drop and note down their names.
  2. 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.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To connect MongoDB with PowerShell, you can use the MongoDB PowerShell module. This module provides cmdlets for interacting with a MongoDB database. To connect to MongoDB using PowerShell, you first need to install the MongoDB PowerShell module using the Power...
To delete documents from MongoDB using Spark Scala, you can follow the following steps:Start by creating a new SparkSession: import org.apache.spark.sql.SparkSession val spark = SparkSession.builder() .appName(&#34;MongoDB Spark Connector&#34;) .config(&#...
To perform a wildcard search with MongoDB and PHP, you can make use of regular expressions and the $regex operator provided by MongoDB. Here&#39;s how you can do it:Establish a connection with your MongoDB server using PHP. You can use the MongoDB\Driver\Manag...