How do I drop a MongoDB database from the command line ?



  • 3 Answer(s)

    We can use the following code:

    mongo <dbname> --eval "db.dropDatabase()"
    

    Further details on scripting the shell from the below link:

    https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#scripting

    Answered on December 21, 2018.
    Add Comment

    This is the excellent way to do it from mongodb console:

    > use mydb;
    > db.dropDatabase();
    

    Here another, we can stop mongod and delete the data files from your data directory, then restart.

    Tips: we can also move the data files to a subfolder, and delete them if we are sure you no longer need them.

    Answered on December 21, 2018.
    Add Comment

    Here there is no need of heredocs or eval,mongo itself can act as an interpreter.

    #!/usr/bin/env mongo
    var db = new Mongo().getDB("someDatabase");
    db.dropDatabase();
    

    Create the file executable and run it.

    Answered on December 21, 2018.
    Add Comment


  • Your Answer

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