Manage functions deployment and runtime options

You can deploy, delete, and modify functions using Firebase CLI commands or by setting runtime options in your functions source code.

Deploy functions

To deploy functions, run this Firebase CLI command:

$ firebase deploy --only functions

By default, the Firebase CLI deploys all of the functions inside index.js at the same time. If your project contains more than 5 functions, we recommend that you use the --only flag with specific function names to deploy only the functions that you've edited. Deploying specific functions this way speeds up the deployment process and helps you avoid running into deployment quotas. For example:

$ firebase deploy --only functions:addMessage,functions:makeUppercase

See the Firebase CLI reference for the full list of available commands.

By default, the Firebase CLI looks in the functions/ folder for the source code. You can specify another folder by adding the following lines in firebase.json:

"functions": {
  "source": "another-folder"
}

Delete functions

You can delete previously deployed functions in two ways:

  • explicitly in the Firebase CLI with functions:delete
  • implictly by removing the function from index.js prior to deployment.

Both operations prompt you to confirm before removing the function from production.

Explicit function deletion in the Firebase CLI supports multiple arguments as well as functions groups, and allows you to specify a function running in a particular region. Also, you can override the confirmation prompt.

# Delete all functions that match the specified name in all regions.
$ firebase functions:delete myFunction

# Delete a specified function running in a specific region.
$ firebase functions:delete myFunction --region us-east-1

# Delete more than one function
$ firebase functions:delete myFunction myOtherFunction

# Delete a specified functions group.
$ firebase functions:delete groupA

# Bypass the confirmation prompt.
$ firebase functions:delete myFunction --force

With implicit function deletion, firebase deploy parses index.js and removes from production any functions that have been removed from the file. Note that you cannot delete functions by deploying in non-interactive mode.

Modify a function's name, region or trigger

If you are renaming or changing the region or trigger for functions that are handling production traffic, follow the steps in this section to avoid losing events during modification. Before you follow these steps, first ensure that your function is idempotent, since both the new version and the old version of your function will be running at the same time during the change.

Rename a function

To rename a function, create a new renamed version of the function in index.js and then run two separate deployment commands. The first command deploys the newly named function, and the second command removes the previously deployed version. For example, if you have a function called webhook that you'd like to change to webhookNew, revise the code as follows:

// before
const functions = require('firebase-functions');

exports.webhook = functions.https.onRequest((req, res) => {
    res.send("Hello");
});

// after
const functions = require('firebase-functions');

exports.webhookNew = functions.https.onRequest((req, res) => {
    res.send("Hello");
});

Then run the following commands to deploy the new function:

# Deploy new function called webhookNew
$ firebase deploy --only functions:webhookNew

# Wait until deployment is done; now both webhookNew and webhook are running

# Delete webhook
$ firebase functions:delete webhook

Change a function's region

If you are changing the region of a function that's handling production traffic, you can prevent event loss by performing these steps in order:

  1. Rename the function, and change its region.
  2. Deploy the renamed function, which results in temporarily running the same code in both regions.
  3. Delete the previous function.

For example, if you have a function called webhook that is currently in the default functions region of us-central1, and you would like to migrate it to asia-northeast1, you should first modify your source code to rename the function and revise the region.

// before
const functions = require('firebase-functions');

exports.webhook = functions
    .https.onRequest((req, res) => {
            res.send("Hello");
    });

// after
const functions = require('firebase-functions');

exports.webhookAsia = functions
    .region('asia-northeast1')
    .https.onRequest((req, res) => {
            res.send("Hello");
    });

Then deploy by running:

$ firebase deploy --only functions:webhookAsia

Now there are two identical functions running: webhook is running in us-central1, and webhookAsia is running in asia-northeast1.

Then, delete webhook:

$ firebase functions:delete webhook

Now there is only one function - webhookAsia, which is running in asia-northeast1.

Change a function's trigger type

As you develop your Cloud Functions for Firebase deployment over time, you may need to change a function's trigger type for various reasons. For example, you might want to:

  • Change from the legacy storage onChange event to onFinalize, onDelete, onArchive, and onMetadataUpdate. (Learn more about this in the beta to v1 migration guide).
  • Change from one type of Firebase Realtime Database or Cloud Firestore event to another one, such as the generic onWrite event to the granular onCreate event.

It is not possible to change a function's event type by just changing the source code and running firebase deploy. To avoid errors, change a function's trigger type by this procedure:

  1. Modify the source code to include a new function with the desired trigger type.
  2. Deploy the function, which results in temporarily running both the old and new functions.
  3. Explicitly delete the old function from production using the Firebase CLI.

For instance, if you had a function objectChanged that has the legacy onChange event type, and you'd like to change it to onFinalize, first rename the function and edit it to have the onFinalize event type.

// before
const functions = require('firebase-functions');

exports.objectChanged = functions.storage.object().onChange((object) => {
    return console.log('File name is: ', object.name);
});

// after
const functions = require('firebase-functions');

exports.objectFinalized = functions.storage.object().onFinalize((object) => {
    return console.log('File name is: ', object.name);
});

Then run the following commands to create the new function first, before deleting the old function:

# Create new function objectFinalized
$ firebase deploy --only functions:objectFinalized

# Wait until deployment is done; now both objectChanged and objectFinalized are running

# Delete objectChanged
$ firebase functions:delete objectChanged

Set runtime options

Cloud Functions for Firebase lets you select runtime options such as the Node.js runtime version and per-function timeout and memory allocation.

Set Node.js version

Firebase SDK for Cloud Functions 2.0.0 and higher allows a selection of Node.js runtime. You can choose to run all functions in a project on only version 6 or only version 8 of Node.js. Note that the Node.js 8 runtime is currently in beta, and that you need firebase-tools 4.0.0 or higher to deploy functions to Node.js 8.

Set the version by adding an engines field to the package.json file that was created in your functions/ directory during initialization. For example, if you prefer to use only version 8, edit package.json to add this line:

  "engines": {"node": "8"}

Alternatively, you can omit the engines field entirely and use Node.js 6 (the default) for all functions.

Set timeout and memory allocation

In some cases, your functions may have special requirements for a long timeout value or a large allocation of memory. You can set these values either in the Google Cloud Console or in the function source code (Firebase only).

To set memory allocation and timeout in functions source code, use the runWith parameter introduced in Firebase SDK for Cloud Functions 2.0.0. This runtime option accepts a JSON object conforming to the RuntimeOptions interface, which defines values for timeoutSeconds and memory. For example, this storage function uses 1GB of memory and times out after 300 seconds:

const runtimeOpts = {
  timeoutSeconds: 300,
  memory: '1GB'
}

exports.myStorageFunction = functions
  .runWith(runtimeOpts)
  .storage
  .object()
  .onFinalize((object) = > {
    // do some complicated things that take a lot of memory and time
  });

The maximum value for timeoutSeconds is 540, or 9 minutes. Valid values for memory are:

  • 128MB
  • 256MB
  • 512MB
  • 1GB
  • 2GB

To set memory allocation and timeout in the Google Cloud Platform Console:

  1. In the Google Cloud Platform Console, select Cloud Functions from the left menu.
  2. Select a function by clicking on its name in the functions list.
  3. Click the Edit icon in the top menu.
  4. Select a memory allocation from the drop-down menu labeled Memory allocated.
  5. Click More to display the advanced options, and enter a number of seconds in the Timeout text box.
  6. Click Save to update the function.

Send feedback about...

Need help? Visit our support page.