Delete All Documents in a Collection in MongoDB Using Python?

To delete all documents from a collection in MongoDB, you can pass an empty object {} to the delete_many() method.

The delete_many() method is generally used to delete multiple records at once from a collection that matches a given condition. But, when we pass an empty document {} to it, it clears the entire collection.

The following example deletes all documents from the student collection:

db.student.delete_many({})

Below is a sample Python program to delete all documents from the student collection:

from pymongo import MongoClient

# Connect to the database
mongoClient = MongoClient('mongodb://localhost:27017/')

# Access the school database
db = mongoClient['school']

# Delete all documents from student collection
db.student.delete_many({})

Delete All Records from a Collection using MongoDB Shell

If you want to delete all records from a collection using the MongoDB shell, you can pass an empty document {} to the deleteMany() method.

The deleteMany() method also works like the delete_many() method of the pymongo package.

The following example deletes all records of the student collection using the deleteMany() method:

db.student.deleteMany({})

Here is a sample image that shows the deletion of records from a collection using MongoDB shell:

Delete all documents from a collection using mongo shell

Deleting Multiple Records from a Collection that Match a Condition

You can also delete multiple records from a collection using the delete_many() method that match a given criteria.

Here is the syntax:

#syntax
db.collectionName.delete_many({<field1>: <value1>, <field2>: <value2>, ... })

The following example deletes all the students from the student collection that has gender=”Male”:

# Delete all documents that has gender='Male'
db.student.delete_many({'gender': 'Male'})

Deleting Only One Record that Matches the Given Condition

If you want to delete only a single document that matches a given condition, you can use the delete_one() method.

The delete_one() method is almost similar to the delete_many() method, the only difference is that it deletes only a single record even if multiple records match the given criteria.

The following example deletes a single record from the student collection that has the status field value “D”:

db.student.delete_one({"status": "D"})

On deletion of the matching record, the delete_one() method returns a instance of the pymongo.results.DeleteResult with the status of the operation i.e. whether the record is deleted or not.


Conclusion

In this article, we learned how we can delete all documents from a collection in MongoDB.

In summary, deleting all records from a collection is quite easy in MongoDB. If you are using the pymongo package of Python, you can pass an empty object {} to the delete_many() method to delete all records from the collection.

If you are using MongoDB shell, you can pass an empty object {} to the deleteMany() method.

Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts