45

I want to drop a database in MongoDB similarly to

use <DBNAME>
db.dropDatabase()

in the Mongo shell.

How do I do that in PyMongo?

2 Answers 2

79

PyMongo 2.4 up to at least 3.11.4

from pymongo import MongoClient
client = MongoClient('<HOST>', <PORT>)
client.drop_database('<DBNAME>')

PyMongo 2.3 and earlier

from pymongo import Connection
connection = Connection('<HOST>', <PORT>)
connection.drop_database('<DBNAME>')
Sign up to request clarification or add additional context in comments.

Comments

7
from pymongo import MongoClient
client = MongoClient('<HOST>', <PORT>)
client.db.command("dropDatabase")

see copydb example: https://api.mongodb.org/python/current/examples/copydb.html

You can also use runCommand helper to run other commands, detail see https://docs.mongodb.org/v3.0/reference/command/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.