Delete Database - PyMongo - Examples


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.

Examples

1. Delete MongoDB database whose name is "organisation"

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.

Python Program

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)

Output

Delete or Drop MongoDB Database using PyMongo
Delete MongoDB Database with Python

Summary

In this PyMongo Tutorial, we learned how to delete a MongoDB database by name using drop_database() function.