How to Delete All Documents in a Collection in Python


In this example we will show how to delete all documents in a collection  using the delete_many() method in Python MongoDB.

Source Code

from pymongo import MongoClient

client = MongoClient("localhost", 27017)
db = client["mydatabase"]
collection = db["us_state"]

a = collection.delete_many({})

print(a.deleted_count, "documents have been deleted")

Output:

7 documents have been deleted

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments