How to drop all databases in MongoDB?
NickName:Ponabenthur Vithushan Ask DateTime:2018-07-25T22:16:39

How to drop all databases in MongoDB?

I have a list of databases in my MongoDB. How to delete all databases except local, admin, and config?
enter image description here

Copyright Notice:Content Author:「Ponabenthur Vithushan」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51521199/how-to-drop-all-databases-in-mongodb

Answers
kevinadi 2018-07-27T06:31:53

You can use the getDBNames() method in the mongo shell.\n\nThis method must be called from the Mongo() instance. Unfortunately I don't think the getDBNames() method is documented.\n\nAfter getting the database names, you can then loop through them to drop the unwanted ones using something like:\n\nMongo().getDBNames().forEach(function(x) {\n // loop through all the database names\n if (['admin', 'config', 'local'].indexOf(x) < 0) {\n // drop database if it's not admin, config, or local\n Mongo().getDB(x).dropDatabase();\n }\n})\n\n\nFor example:\n\n> show dbs\nadmin 0.000GB\nconfig 0.000GB\nlocal 0.001GB\ntest 0.000GB\ntest2 0.000GB\ntest3 0.000GB\n\n> Mongo().getDBNames().forEach(function(x) {\n... if (['admin', 'config', 'local'].indexOf(x) < 0) {\n... Mongo().getDB(x).dropDatabase();\n... }\n... })\n\n> show dbs\nadmin 0.000GB\nconfig 0.000GB\nlocal 0.001GB\n",


More about “How to drop all databases in MongoDB?” related questions

How to drop all databases in MongoDB?

I have a list of databases in my MongoDB. How to delete all databases except local, admin, and config?

Show Detail

PHP 7 MongoDB - drop all databases

I'm trying to unit test my mongodb methods, thus I need a tearDown() method to flush my entire mongo database after testing. Here is the function: public function tearDown() { $databases = $t...

Show Detail

How to drop all InfluxDB databases?

I am trying to drop all of the InfluxDB databases except the _internal one as I do not want to do this every time manually, however, this is not supported directly from influxdb. I was considering

Show Detail

Drop all databases that start with a specific string on Mongo shell

I have a huge list of mongodb databases and I want to remove all databases that start with the string &quot;2019&quot; using mongo shell command I found the command to list them db.adminCommand( {

Show Detail

Drop empty databases from MongoDB

I have a bunch of empty databases showing up in my MongoDB (from time to time, and for an unknown reason) and I'm looking for an easy way to drop all of the empty ones. Here is a piece of the resul...

Show Detail

Drop all databases in MySQL

We have many databases in our MySQL and we want to shrink/purge ibdata1 file in MySQL. How can we drop all the databases from the MySQL except information_schema and mysqld databases?

Show Detail

drop all databases at the same time

I want to drop all of my databases at the same time and I have done this function so far but unfortunately, it doesn't work and gives me no error at all nothing. import mysql.connector def drop_da...

Show Detail

MongoDB permissions: restrict access to two databases

I am trying to implement some restrictions on my MongoDB server: Two databases on my server should be restricted regarding delete/drop operations - only a special user account should be allowed to...

Show Detail

Drop multiple Mongo Databases using the Node JS Driver

I am able to dropDatabase using the Node JS Driver Db class Method dropDatabase. But the problem is, I have to connect to the Database before dropping it as follows: var MongoClient = require('m...

Show Detail

Fastest way to reset MongoDB server / drop all databases

I am starting a test MongoDB server using MongoBox and before each test method I am resetting it: class BaseTest(unittest.TestCase): def setUp(self): self.purge_database() @

Show Detail