Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions firestore/cloud-async-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
25 changes: 7 additions & 18 deletions firestore/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down