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 gcloud command-line tool or the Google Cloud Console to perform invalidations.

You can access the gcloud command-line tool in two ways:

  • Install the Cloud SDK by following the instructions at Installing with apt-get. If you haven't used the gcloud command-line tool 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: example.com/images/file.jpg).
  5. Click Invalidate.

gcloud

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

The external HTTP(S) load balancer's name and the URL map's name are the same.

Replace LOAD_BALANCER_NAME with the name of your load balancer. To list your load balancers, use the gcloud compute url-maps list command.

Optional: To invalidate for only one host, add the --host flag (--host example.com).

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

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

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: example.com/images/*).
  5. Click Invalidate.

gcloud

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

Replace LOAD_BALANCER_NAME with the name of your load balancer. To list your load balancers, use the gcloud compute url-maps list command.

Optional: To invalidate for only one host, add the --host flag (--host example.com).

gcloud compute url-maps invalidate-cdn-cache LOAD_BALANCER_NAME \
    --host HOSTNAME \
    --path "/images/*"

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

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: example.com/*).
  5. Click Invalidate.

gcloud

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

Replace the following:

  • LOAD_BALANCER_NAME: the name of your load balancer
  • /*: the URL path that you want to invalidate; does not include the hostname; the path can use an explicit filename or a wildcard

Optional: To invalidate for only one host, add the --host flag (--host example.com).

gcloud compute url-maps invalidate-cdn-cache LOAD_BALANCER_NAME \
    --host HOSTNAME \
    --path "/*"

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

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.