Schedule functions


If you want to schedule functions to run at specified times, use the onSchedule handler provided by firebase-functions/v2/scheduler. These functions use Cloud Scheduler to invoke function logic at the times or intervals that you define.

Before you begin

To use this solution in your Firebase project, your project must be on the Blaze pricing plan. If it's not already on the Blaze plan, upgrade your pricing plan.

Though billing is required, you can expect the overall cost to be manageable, as each Cloud Scheduler job costs $0.10 (USD) per month, and there is an allowance of three jobs per Google account, at no charge. Use the Blaze pricing calculator to generate a cost estimate based on your projected usage.

The Cloud Scheduler API must be enabled for your project. It should already be enabled for most Firebase projects; you can verify in the Google Cloud Platform Console.

Write a scheduled function

In Cloud Functions for Firebase, scheduling logic resides in your functions code, with no special deploy-time requirements. For example, to clean up inactive user accounts once daily, you could write a function starting with the following import statements:

Node.js

// The Cloud Functions for Firebase SDK to set up triggers and logging.
const {onSchedule} = require("firebase-functions/v2/scheduler");
const {logger} = require("firebase-functions");

// The Firebase Admin SDK to delete inactive users.
const admin = require("firebase-admin");
admin.initializeApp();

// The es6-promise-pool to limit the concurrency of promises.
const PromisePool = require("es6-promise-pool").default;
// Maximum concurrent account deletions.
const MAX_CONCURRENT = 3;

Python

# The Cloud Functions for Firebase SDK to set up triggers and logging.
from firebase_functions import scheduler_fn

# The Firebase Admin SDK to delete users.
import firebase_admin
from firebase_admin import auth

firebase_admin.initialize_app()

Then, you could use onSchedule to start a Cloud Scheduler task:

Node.js

// Run once a day at midnight, to clean up the users
// Manually run the task here https://console.cloud.google.com/cloudscheduler
exports.accountcleanup = onSchedule("every day 00:00", async (event) => {
  // Fetch all user details.
  const inactiveUsers = await getInactiveUsers();

  // Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.
  const promisePool = new PromisePool(
      () => deleteInactiveUser(inactiveUsers),
      MAX_CONCURRENT,
  );
  await promisePool.start();

  logger.log("User cleanup finished");
});

Python

# Run once a day at midnight, to clean up inactive users.
# Manually run the task here https://console.cloud.google.com/cloudscheduler
@scheduler_fn.on_schedule(schedule="every day 00:00")
def accountcleanup(event: scheduler_fn.ScheduledEvent) -> None:
    """Delete users who've been inactive for 30 days or more."""
    user_page: auth.ListUsersPage | None = auth.list_users()
    while user_page is not None:
        inactive_uids = [
            user.uid for user in user_page.users if is_inactive(user, timedelta(days=30))
        ]
        auth.delete_users(inactive_uids)
        user_page = user_page.get_next_page()

Both Unix Crontab and App Engine syntax are supported by Cloud Scheduler. For example, to use Crontab, do something like this:

Node.js

exports.scheduledFunctionCrontab = onSchedule("5 11 * * *", async (event) => {
  // ...
});

Python

@scheduler_fn.on_schedule(schedule="5 11 * * *")

Deploy a scheduled function

When you deploy a scheduled function, a scheduler job and an HTTP function are created automatically. The Firebase CLI echoes the function name, and you can view the job and the function in the GCP Console. The topic is named according to the following convention:

firebase-schedule-function_name-region

For example:

firebase-schedule-accountcleanup-us-east1.

At the scheduled time, the default compute service account invokes the associated HTTP function. This means that only the associated Cloud Scheduler job has permission to run the function.