I need to use pymongo to do bulk delete for mongodb. I'm getting the _id field of the documents that I need deleted with a query but I'm not able to figure out how to use the _id's I get to be deleted in chunks of 10,000.
Asked
Active
Viewed 2,830 times
4
-
Please include some code and maybe what the db looks like so we can help you better. Have you also tried looking at the docs? possibly [deleteMany()](https://docs.mongodb.com/manual/reference/method/db.collection.deleteMany/) would be down your alley. – Jab Dec 10 '18 at 21:21
1 Answers
6
Below is an example of Unordered Bulk Write Operations using current PyMongo v3.7.2:
from pymongo import DeleteOne
from pymongo.errors import BulkWriteError
requests = [
DeleteOne({'_id': 101}),
DeleteOne({'_id': 102})]
try:
db.collection.bulk_write(requests, ordered=False)
except BulkWriteError as bwe:
pprint(bwe.details)
The example above is using the unordered
operations, because unordered bulk operations are batched and sent to the server in arbitrary order where they may be executed in parallel. Any errors that occur are reported after all operations are attempted. See also PyMongo Bulk Write Operations and MongoDB Bulk Write Operations for more information.

Wan B.
- 18,367
- 4
- 54
- 71
-
I have tried this but seems it's not working I found when use DeleteOne we should ```from bson import ObjectId``` and : ```DeleteOne({'_id': ObjectId(101)})``` – panda912 Jul 28 '22 at 01:57