InMongoDB, deletion is key to efficient data management. PyMongo offers methods to delete a single document, multiple documents, all documents in a collection or the entire collection itself. Here's a quick overview of these techniques:
1. Delete a single document: Use delete_one() to remove only the first document that matches the filter.
res = my_collection.delete_one({"name": "Mr.Geek"})
To see the number of documents deleted :
print(res.deleted_count)
2. Delete Multiple Documents:Use delete_many() to remove all documents that match a specific condition.
res = my_collection.delete_many({"name": "Mr.Geek"})
To see the number of documents deleted :
print(res.deleted_count)
3. Delete All Documents in a Collection: Using delete_many({}): This deletes all documents in the collection but keeps the collection and its indexes.
res = my_collection.delete_many({})
To see the number of documents deleted :
print(res.deleted_count)
4. Drop the entire collection: If you want to remove all documents along with the collection and its indexes, use drop().
db.my_collection.drop()
Examples
Example 1: Delete a single document
PythonfrompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c["my_database"]col=db["my_collection"]col.insert_many([{"name":"Mr.Geek","role":"Developer"},{"name":"Mr.Geek","role":"Designer"}])res=col.delete_one({"name":"Mr.Geek"})print(res.deleted_count)
Output
1
Explanation:
- delete_one()deletes only the first document that matches thefilter { "name": "Mr.Geek" }. Even if multiple documents match, only one is removed.
- deleted_count shows how many documents were deleted (expected: 1).
Example 2: Delete multiple documents
PythonfrompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c["my_database"]col=db["my_collection"]col.insert_many([{"name":"Mr.Geek","role":"Developer"},{"name":"Mr.Geek","role":"Tester"},{"name":"Mr.Geek","role":"Manager"}])res=col.delete_many({"name":"Mr.Geek"})print(res.deleted_count)
Output
4
Explanation:
- delete_many() deletes all documents matching the filter { "name": "Mr.Geek" }.
- deleted_count shows how many documents were deleted.
Example 3: Delete all documents in a collection
PythonfrompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c["my_database"]col=db["my_collection"]col.insert_many([{"product":"Laptop"},{"product":"Phone"},{"product":"Tablet"}])res=col.delete_many({})print(res.deleted_count)
Output
3
Explanation:
- Passing an emptyfilter {} to delete_many() means “match all documents”. This will remove every document in the collection.
- deleted_count will show the total deleted (expected: 3).
Example 4: Drop the entire collection
PythonfrompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c["my_database"]col=db["my_collection"]col.insert_many([{"product":"Laptop"},{"product":"Phone"},{"product":"Tablet"}])db.my_collection.drop()print("Collection dropped successfully.")
Output
Collection dropped successfully.
Explanation:
- .drop()deletes the entire collection, including all documents and indexes.
- db.my_collection.drop()and col.drop() are functionally identical.
Related articles