Invalidate cached content

This page describes how to invalidate your Cloud CDN cached content.

For example, if a file located at /images/file.jpg has been cached and needs to be invalidated, you can use several methods to invalidate it, depending on whether you want to affect only that file or a wider scope. In each case, you can invalidate for all hostnames or for only one hostname.

To learn more about cache invalidation, see Cache invalidation overview.

Before you begin

You can use the Google Cloud CLI or the Google Cloud console to perform invalidations.

You can access the Google Cloud CLI in two ways:

  • Install the Google Cloud CLI by following the instructions at Installing with apt-get. If you haven't used the Google Cloud CLI previously, first run gcloud init to authenticate.

  • Use Cloud Shell, which comes with gcloud already installed.

To create a custom role with the required permissions, see Creating and managing custom roles.

To add a predefined role, use the IAM quickstart.

Invalidate only one file

Console

  1. In the Google Cloud console, go to the Cloud CDN page.

    Go to the Cloud CDN page

  2. Under Associated load balancers, click the load balancer name.
  3. Click the Cache invalidation tab.
  4. Enter the path to the file.
    • If you want to invalidate a path for all hostnames, enter only the path (for example: /images/file.jpg).
    • If you want to invalidate a path for only one hostname, specify the hostname as part of the path (for example: host1.com/images/file.jpg).
  5. Click Invalidate.

gcloud

Use the gcloud compute url-maps invalidate-cdn-cache command.

To specify only one file for invalidation, use the --path flag with the filename.

gcloud compute url-maps invalidate-cdn-cache URL_MAP_NAME \
    --path "/images/file.jpg"

Replace URL_MAP_NAME with the name of your URL map. To list your URL maps, use the gcloud compute url-maps list command.

To invalidate for only one host, add the --host flag—for example, --host host1.com.

gcloud compute url-maps invalidate-cdn-cache URL_MAP_NAME \
    --host host1.com \
    --path "/images/file.jpg"

By default, the Google Cloud CLI waits until the invalidation has completed. To perform the invalidation in the background, append --async to the command line.

API

Use the urlMaps.invalidateCache method API call.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/urlMaps/URL_MAP_NAME/invalidateCache

Replace the following:

  • PROJECT_ID: your Google Cloud console project ID
  • URL_MAP_NAME: the name of the URL map

To specify a file for invalidation, use the following sample JSON request body:

{
  "path": "/images/file.jpg"
}

To invalidate the file for only one host, use the following sample JSON request body:

{
  "host": "host1.com",
  "path": "/images/file.jpg"
}

Invalidate the whole directory

Console

  1. In the Google Cloud console, go to the Cloud CDN page.

    Go to the Cloud CDN page

  2. Under Associated load balancers, click the load balancer name.
  3. Click the Cache invalidation tab.
  4. Enter the directory path and wildcard (/path/to/file/*).
    • If you want to invalidate the whole directory for all hostnames, enter only the path and wildcard (for example: /images/*).
    • If you want to invalidate the whole directory for only one hostname, specify the hostname as part of the path (for example: host1.com/images/*).
  5. Click Invalidate.

gcloud

Use the gcloud compute url-maps invalidate-cdn-cache command.

gcloud compute url-maps invalidate-cdn-cache URL_MAP_NAME \
    --path "/images/*"

Replace URL_MAP_NAME with the name of your URL map.

To invalidate for only one host, add the --host flag—for example, --host host1.com.

gcloud compute url-maps invalidate-cdn-cache URL_MAP_NAME \
    --host host1.com \
    --path "/images/*"

API

Use the urlMaps.invalidateCache method API call.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/urlMaps/URL_MAP_NAME/invalidateCache

Replace the following:

  • PROJECT_ID: your Google Cloud console project ID
  • URL_MAP_NAME: the name of the URL map

To specify a directory for invalidation, use the following sample JSON request body:

{
  "path": "/images/*"
}

To invalidate a directory for only one host, use the following sample JSON request body:

{
  "host": "host1.com",
  "path": "/images/*"
}

Invalidate by cache tags

Console

Using cache tags for invalidation isn't supported in the Google Cloud console.

gcloud

Use the gcloud beta compute url-maps invalidate-cdn-cache command.

gcloud beta compute url-maps invalidate-cdn-cache URL_MAP_NAME \
    --tags=TAGS

Replace the following:

  • URL_MAP_NAME: the name of the URL map
  • TAGS: a tag or a comma-separated list of tags; alternatively, you can specify this flag up to 10 times

For example, if the backend response includes a Cache-Tag header with an opaque user ID, such as a UUID4 or another form of GUID, you can invalidate all content for a specific user by using the tag flag. Additionally, to invalidate content for only one host, such as for your staging environment, add the --host flag.

gcloud beta compute url-maps invalidate-cdn-cache url_map_1 \
    --tags="1115b8f4-f804-4861-9629-8cb9aecdeeb3" \
    --host="host1.com"

API

Use the urlMaps.invalidateCache method API call.

POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/global/urlMaps/URL_MAP_NAME/invalidateCache

Replace the following:

  • PROJECT_ID: your Google Cloud console project ID
  • URL_MAP_NAME: the name of the URL map

To specify cache tags for invalidation, use the following sample JSON request body:

{
  "tags": "tag1,tag2"
}

You can specify a single tag or a comma-separated list of tags.

To invalidate by cache tags for only one host, use the following sample JSON request body:

{
  "host": "host1.com",
  "path": "tag1,tag2"
}

Invalidate everything

Console

  1. In the Google Cloud console, go to the Cloud CDN page.

    Go to the Cloud CDN page

  2. Under Associated load balancers, click the load balancer name.
  3. Click the Cache invalidation tab.
  4. Enter only the root and the wildcard (/*).
    • If you want to invalidate everything for all hostnames, enter only /* into the field.
    • If you want to invalidate everything for only one hostname, specify the hostname as part of the path (for example: host1.com/*).
  5. Click Invalidate.

gcloud

Use the gcloud compute url-maps invalidate-cdn-cache command.

gcloud compute url-maps invalidate-cdn-cache URL_MAP_NAME \
    --path "/*"

Replace the following:

  • URL_MAP_NAME: the name of the URL map
  • /*: the URL path that you want to invalidate; doesn't include the hostname; the path can use an explicit filename or a wildcard

To invalidate for only one host, add the --host flag—for example, --host host1.com.

gcloud compute url-maps invalidate-cdn-cache URL_MAP_NAME \
    --host host1.com \
    --path "/*"

API

Use the urlMaps.invalidateCache method API call.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/urlMaps/URL_MAP_NAME/invalidateCache

Replace the following:

  • PROJECT_ID: your Google Cloud console project ID
  • URL_MAP_NAME: the name of the URL map

To specify a URL path for invalidation, use the following sample JSON request body:

{
  "path": "/*"
}

To invalidate the URL path for only one host, use the following sample JSON request body:

{
  "host": "host1.com",
  "path": "/*"
}

See the status of your invalidation request

Cloud CDN emits two log lines per invalidation, one when the invalidation is accepted and the other when it's complete. The first has the host and path information.

To see the status of your invalidation request or find a definitive list of recently submitted requests, you can use Cloud Logging or Cloud Monitoring.

The following is a sample command that uses Logging:

gcloud logging read 'protoPayload.methodName="v1.compute.urlMaps.invalidateCache"' \
    --limit=10

The following is a sample query that uses Monitoring:

protoPayload.serviceName="compute.googleapis.com"
protoPayload.methodName="v1.compute.urlMaps.invalidateCache"

What's next

  • To check whether Cloud CDN is serving responses from cache, see Viewing logs.
  • To learn about which content is cacheable or non-cacheable, see Caching overview.