Skip to content Skip to sidebar Skip to footer

How To Efficiently Delete Old Firebase Cloud Documents Using A Google Cloud Function?

I am currently using the following scheduled Cloud Function to delete any Assignments or Duties (documents) older than 24 hours. The function works fine, and the documents successf

Solution 1:

As Doug explained in his comment and in the official video series, your code must return a promise that resolves only after all the asynchronous work is complete, else the Cloud Function will get terminated early.

You can adapt your code as follows, by using Promise.all():

exports.scheduledCleanOldDutiesCollection = functions.pubsub.schedule('0 3 * * *')
    .timeZone('America/Chicago')
    .onRun((context) => {
        const dateYesterday = newDate(newDate().getTime() - (24 * 60 * 60 * 1000));   // 24 hoursreturn db.collectionGroup('duties').where('date', '<', dateYesterday).get()
            .then(querySnapshot => {
                const promises = [];
                querySnapshot.forEach(doc => {
                    promises.push(doc.ref.delete());
                });
                returnPromise.all(promises);
            });
    });

Post a Comment for "How To Efficiently Delete Old Firebase Cloud Documents Using A Google Cloud Function?"