From 74d41afc10229b9041da4953a8441b089af91733 Mon Sep 17 00:00:00 2001 From: David del Real Sifuentes Date: Tue, 3 Feb 2026 16:17:38 +0000 Subject: [PATCH] fix: Update firestore samples for python to use recurssive delete b/470275866 Modified samples for Firestore Client and Asyn Firestore Client. --- firestore/cloud-async-client/snippets.py | 20 ++++++------------- firestore/cloud-client/snippets.py | 25 +++++++----------------- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/firestore/cloud-async-client/snippets.py b/firestore/cloud-async-client/snippets.py index b0a97962cc5..3a7b9476941 100644 --- a/firestore/cloud-async-client/snippets.py +++ b/firestore/cloud-async-client/snippets.py @@ -693,24 +693,16 @@ async def delete_full_collection(): db = firestore.AsyncClient() # [START firestore_data_delete_collection_async] - async def delete_collection(coll_ref, batch_size): - docs = coll_ref.limit(batch_size).stream() - deleted = 0 + async def delete_collection(coll_ref): - async for doc in docs: - print(f"Deleting doc {doc.id} => {doc.to_dict()}") - await doc.reference.delete() - deleted = deleted + 1 - - if deleted >= batch_size: - return delete_collection(coll_ref, batch_size) + await db.recursive_delete(coll_ref) # [END firestore_data_delete_collection_async] - await delete_collection(db.collection("cities"), 10) - await delete_collection(db.collection("data"), 10) - await delete_collection(db.collection("objects"), 10) - await delete_collection(db.collection("users"), 10) + await delete_collection(db.collection("cities")) + await delete_collection(db.collection("data")) + await delete_collection(db.collection("objects")) + await delete_collection(db.collection("users")) async def collection_group_query(db): diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index 09dff308a50..9bc64d1c383 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -839,28 +839,17 @@ def delete_full_collection(): db = firestore.Client() # [START firestore_data_delete_collection] - def delete_collection(coll_ref, batch_size): - if batch_size == 0: - return + def delete_collection(coll_ref): - docs = coll_ref.list_documents(page_size=batch_size) - deleted = 0 - - for doc in docs: - print(f"Deleting doc {doc.id} => {doc.get().to_dict()}") - doc.delete() - deleted = deleted + 1 - - if deleted >= batch_size: - return delete_collection(coll_ref, batch_size) + print(f"Recursively deleting collection: {coll_ref}") + db.recursive_delete(coll_ref) # [END firestore_data_delete_collection] - delete_collection(db.collection("cities"), 10) - delete_collection(db.collection("data"), 10) - delete_collection(db.collection("objects"), 10) - delete_collection(db.collection("users"), 10) - delete_collection(db.collection("users"), 0) + delete_collection(db.collection("cities")) + delete_collection(db.collection("data")) + delete_collection(db.collection("objects")) + delete_collection(db.collection("users")) def collection_group_query(db):