1

I want to create a firebase function that would delete or update a document after 24 hours like Instagram stories. Would this implementation work?

// 24 hours in miliseconds
const time = 24 * 60 * 60 * 1000;

exports.updateUser = functions.firestore
  .document('users/{id}')
  .onUpdate((change, context) => {
    setTimeout(() => {
      // Delete or update doc
    }, time);
  });
baymax
  • 3,836
  • 2
  • 10
  • 30

1 Answers1

2

If you are asking if you can use setTimeout to implement a one-hour delay in Cloud Functions, the answer is no, it will not work. Cloud Functions can run for at most 9 minutes (60s default). After that, the function will shut down, and your timeout callback will be canceled.

If you want to schedule some work for a specific future time, you should use Cloud Tasks instead. Either that, or use a scheduled function to periodically scan for documents to delete.

See also:

Doug Stevenson
  • 268,359
  • 30
  • 341
  • 380