To remove a whole collection using Cloud Functions, you can use the batch()
method to create a batch operation and then use the delete()
method to remove each document within the collection.
Here's an example code snippet:
const admin = require('firebase-admin');
exports.deleteCollection = functions.https.onCall(async (data, context) => {
const collectionRef = admin.firestore().collection(data.collectionPath);
const batch = admin.firestore().batch();
const docs = await collectionRef.get();
docs.forEach(doc => {
batch.delete(doc.ref);
});
await batch.commit();
return `Collection ${data.collectionPath} successfully deleted.`;
});
In this example, the Cloud Function is triggered when the deleteCollection
function is called. The function takes a collectionPath
parameter, which is the path of the collection to be deleted.
The function starts by creating a reference to the collection using admin.firestore().collection()
method. It then creates a batch operation using admin.firestore().batch()
method.
Next, the function retrieves all the documents in the collection using collectionRef.get()
method and loops through each document using docs.forEach()
method. For each document, it adds a delete operation to the batch using batch.delete(doc.ref)
method.
Finally, the function commits the batch operation using batch.commit()
method to remove all the documents within the collection. The function returns a success message once the operation is completed.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-01-13 11:00:00 +0000
Seen: 11 times
Last updated: Feb 14
Can the batch set init function be configured to differentiate between upper and lower case letters?
What is the method to modify the data of a firestore document using a firebase cloud function?
What does "Read timed out" mean in relation to a Batch Request in the Google Calendar API?
How can I run several scripts when the application starts?
How to create an empty file using a batch file?
What is the process for incorporating batch processing into google apps script?
How can multiple recently uploaded files be selected using a batch file?
Can the time zone be configured to Europe/Berlin in Google Cloud Scheduler?