Skip to content
Merged
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
38 changes: 5 additions & 33 deletions firestore/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -947,37 +947,9 @@ async function multipleCursorConditions(db) {
}

// [START firestore_data_delete_collection]
async function deleteCollection(db, collectionPath, batchSize) {
async function deleteCollection(db, collectionPath) {
const collectionRef = db.collection(collectionPath);
const query = collectionRef.orderBy('__name__').limit(batchSize);

return new Promise((resolve, reject) => {
deleteQueryBatch(db, query, resolve).catch(reject);
});
}

async function deleteQueryBatch(db, query, resolve) {
const snapshot = await query.get();

const batchSize = snapshot.size;
if (batchSize === 0) {
// When there are no documents left, we are done
resolve();
return;
}

// Delete documents in a batch
const batch = db.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();

// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(db, query, resolve);
});
return await db.recursiveDelete(collectionRef);
}

// [END firestore_data_delete_collection]
Expand Down Expand Up @@ -1019,7 +991,7 @@ describe('Firestore Smoketests', () => {
});

it('should delete existing documents', () => {
return deleteCollection(db, 'cities', 50);
return deleteCollection(db, 'cities');
});

it('should store example data', () => {
Expand Down Expand Up @@ -1081,7 +1053,7 @@ describe('Firestore Smoketests', () => {
it('should handle transaction with a result', () => {
return transactionWithResult(db).then(res => {
// Delete data set
return deleteCollection(db, 'cities', 50);
return deleteCollection(db, 'cities');
});
});

Expand Down Expand Up @@ -1178,7 +1150,7 @@ describe('Firestore Smoketests', () => {
});

it('should delete the whole collection', () => {
return deleteCollection(db, 'cities', 50);
return deleteCollection(db, 'cities');
});

it('should find all museums when querying a collection group', () => {
Expand Down
Loading