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? 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",