Varnish Logo

Invalidating Cache

Instantaneously clear cached content across the global edge network using manual purges or API-driven invalidation requests.

Varnish CDN provides a way to invalidate cached content using purge requests. This allows you to remove specific content from the cache when it has been updated or is no longer valid. For example if the origin has updated content, the cache might still serve the old content until it expires based on the caching rules. By sending a purge request, you can ensure that the updated content is served to users immediately.

Varnish CDN has an optimised way of removing cached content based on keys.

Purge Requests

A purge request is basically an HTTP request with the PURGE method. When a purge request is sent to Varnish CDN, it will look for cached content that matches the specified keys and remove it from the cache.

To purge content, you need to send a PURGE request to the Varnish CDN purger endpoint. The request must include the host or path or a combination of host and path (hostpath). As well as the appropriate authentication headers using a Purge Token (see Purge Tokens for more information).

The URL to purge content is in the following format:

https://purger.varnish-cdn.com/api/v1/invalidations/basic/<your-domain>

Where <your-domain> is one of the domains you access to via your Varnish CDN service. The reason for having a domain in the URL is that some systems do not support custom headers and the domain is needed to route the purge request to the correct service.

Purge Keys

The keys used to identify the cached content to be purged are specified in the X-Vcdn-Keys header. The keys can be a combination of host and path called hostpath. The key and the value are separated by a colon (:). For example, to purge a specific path on a domain, you can use the following header:

X-Vcdn-Keys: path:/content/my-image.png

This will purge the cached content for the path /content/my-image.png on the specified domain. To purge all content for a specific domain, you can use the following header:

X-Vcdn-Keys: host:www.example.com

This will purge all cached content for the domain www.example.com. You can also combine both host and path to purge a specific path on a specific domain:

X-Vcdn-Keys: hostpath:www.example.com/content/my-image.png

This will purge the cached content for the path /content/my-image.png on the domain www.example.com.

It is also possible to combine multiple keys in a single purge request by separating them with commas:

X-Vcdn-Keys: host:www.example.com,host:www.another-domain.com
X-Vcdn-Keys: hostpath:www.example.com/content/my-image.png,hostpath:www.another-domain.com/content/another-image.png

Examples

Examples using curl (replace <your-jwt-token> with the actual token and <your-domain> and <path-to-purge> with the appropriate values).

Purge specific path

terminal
export PURGER_URL="https://purger.varnish-cdn.com/api/v1/invalidations/basic" \
curl -X PURGE "$PURGER_URL/<your-domain>/<path-to-purge>" \
  -H "Authorization: Bearer <your-jwt-token>" \
  -H "X-Vcdn-Keys: path:/content/my-image.png" \

Purge entire domain

terminal
export PURGER_URL="https://purger.varnish-cdn.com/api/v1/invalidations/basic" \
curl -X PURGE "$PURGER_URL/<your-domain>" \
    -H "Authorization: Bearer <your-jwt-token>" \
    -H "X-Vcdn-Keys: host:www.example.com" \

Purge specific path on specific domain

terminal
export PURGER_URL="https://purger.varnish-cdn.com/api/v1/invalidations/basic" \
curl -X PURGE "$PURGER_URL/<your-domain>/<path-to-purge>" \
  -H "Authorization: Bearer <your-jwt-token>" \
  -H "X-Vcdn-Keys: hostpath:www.example.com/content/my-image.png" \

Invalidation Using Dashboard

It is also possible to invalidate cache using the Varnish CDN dashboard. This can be useful for quick invalidations without needing to set up purge requests. To invalidate cache using the dashboard, follow these steps:

When purging via the dashboard, there is no need for a purge token as the dashboard will handle the authentication automatically.
  1. Log in to the Varnish CDN dashboard.
  2. Navigate to the "Services" section and select the service you want to invalidate cache for.
  3. Click the "Purge" drop-down.
  4. Choose either "By Path" or "All cache" depending on your needs.
  5. Follow the instructions to complete the invalidation.