0
votes

I've a collection named XYZ in my firestore. And there are 500 documents with different fields in it. I have to delete multiple documents using a where clause from the collection.

cred = credentials.Certificate('XXXX')
app = firebase_admin.initialize_app(cred)
db = firestore.Client()

batch = db.batch()

doc_ref = db.collection('collection_name').where(u'month', '==', 07).get()

for doc in doc_ref:
      batch.delete(doc)

batch.commit()

I tried this but ending up with an error

AttributeError: AttributeError: 'DocumentSnapshot' object has no attribute '_document_path'

Looking for help!

1
You have to actually execute the query with get() and get document references from the results in order to pass them to batch.delete(). You can't pass a Query object. - Doug Stevenson
No, It didn't work. - astroboy
If you want help with that, you should show what you tried, and explain what didn't work the way you expect. - Doug Stevenson
Please edit the question to show your updated code. Don't leave it in a comment. Use the edit link at the bottom of the question. - Doug Stevenson
Edited. You can check the complete snippet that I've tried. - astroboy

1 Answers

3
votes

You are passing a DocumentSnapshot object to batch.delete(), which is not allowed. You must pass a DocumentReference object instead, which can be found in a property of a DocumentSnapshot.

  batch.delete(doc.reference)