How to Delete Database in MongoDB using Python?

Python MongoDB Delete Database

To delete a MongoDB Database in Python language, we shall use PyMongo. Follow these steps to delete a specific MongoDB database.

  1. Create a MongoDB Client to the MongoDB instance.
  2. Use drop_database() function on the client object with the database name passed as argument.

PyMongo Example

In the following example, we shall delete organisation database. Also, for understanding, we will print the list of databases present in the mongod instance before and after deleting the database.

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")

print("List of databases before deletion\n--------------------------")
for x in myclient.list_database_names():
  print(x)
  
#delete database named 'organisation'
myclient.drop_database('organisation')

print("\nList of databases after deletion\n--------------------------")
for x in myclient.list_database_names():
  print(x)
Run
Delete or Drop MongoDB Database using PyMongo
Delete MongoDB Database with Python