I am using Google Cloud Function, but since it runs on a older version of Node, I can not use this answer anymore. I want a function that will batch delete all the documents in a collection and returns the data from it. This is my attempt:
function deleteCollectionAndReturnResults(db, collectionRef, batchSize) {
var query = collectionRef.limit(batchSize);
return deleteQueryBatch(db, query, batchSize, []);
}
function deleteQueryBatch(db, query, batchSize, results) {
return query.get().then(snapshot => {
if (snapshot.size == 0) return 0;
var batch = db.batch();
snapshot.docs.forEach(doc => {
if (doc.exists) {results.push(doc);}
batch.delete(doc.ref);
});
return batch.commit().then(() => snapshot.size);
}).then(function(numDeleted) {
if (numDeleted >= batchSize) {
return deleteQueryBatch(db, query, batchSize, results);
}else{
return results
}
});
}
But when I run it like this:
exports.tester = functions.firestore.document('x/{x}').onCreate(event => {
deleteCollectionAndReturnResults(db, db.collection("x"), 100).then(docs => {
console.log(docs)
})
})
This is my output:
Is there something wrong why I do get the 'function returned undefined'?