Delete everything in a MongoDB database



  • 3 Answer(s)

    For the mongo shell:

    use [database];
    db.dropDatabase();
    

    Ruby code is pretty similar.

     

    Answered on December 22, 2018.
    Add Comment

    More, from the command line:

    mongo DATABASE_NAME --eval "db.dropDatabase();"
    
    

     

    Answered on December 22, 2018.
    Add Comment

    For that problem, when I needed to reset all the collections but didn’t want to loose any database users. Use the following line of code, if you would like to save the user configuration for the database:

    use <whichever database>
    db.getCollectionNames().forEach(function(c) { if (c.indexOf("system.") == -1) db[c].drop(); })
    

    The above code will be go through all collection names from one database and drop those which do not start with “system.”.

    Answered on December 22, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.