Dropping a MongoDB Database from the Command Line
If you need to remove a MongoDB database, you can do so quickly using the command line. This guide will show you how to execute this operation using the MongoDB shell.
Prerequisites
Ensure you have MongoDB installed and accessible from your command line. If you are using MongoDB version 6.0 or later, you will be using mongosh instead of the older mongo shell.
Steps to Drop a Database
Open your terminal.
Run the following command to drop the database:
For MongoDB versions prior to 6.0:
mongo <dbname> --eval "db.dropDatabase()"For MongoDB version 6.0 and later:
mongosh --eval "use <dbname>" --eval "db.dropDatabase()"Replace
<dbname>with the name of the database you wish to delete.
Important Notes
- Dropping a database is irreversible; ensure you have backups if necessary.
- You can verify the database has been dropped by listing the databases with:
mongo --eval "show dbs"
By following these steps, you can efficiently manage your MongoDB databases directly from the command line.