Learn how to purge resources cached by Cloudflare using the purge methods available for your Cloudflare plan.
Overview
The Caching app in your Cloudflare dashboard allows purge of cached resources by single-file (recommended), hostname or cache-tag (Enterprise plans only), or all cached content. All customers can purge by single-file (by URL) or purge all cached assets. Cloudflare Enterprise customers can also purge using hostnames and cache-tags.
Purge by single-file (by URL)
When you choose the Purge by URL (single-file) option in the Caching app, that cached resource is immediately removed from the stored assets in your Content Delivery Network (CDN) across all data centers. Any new request for the purged asset gets the latest version from your origin web server and adds it back to your CDN cache within the specific Cloudflare data center that served the request.
Important! A single-file purge performed through your Cloudflare dashboard does not clear objects that contain:
- Custom cache keys
- An origin header
- Any of these response headers:
- X-Forwarded-Host
- X-Host
- X-Forwarded-Scheme
- X-Original-URL
- X-Rewrite-URL
- Forwarded
You can purge objects with these characteristics using an API call (Purge files by URL).
In the data/header section of the API call, you must include all headers and cache keys contained in the cached resource, along with their matching values.
Purging by single-file through your Cloudflare dashboard
1. Login to Cloudflare and select the site with the resources to purge.
2. Click the Caching app.
3. In the Purge Cache pane, click Custom Purge.
The Custom Purge dialog box appears.
4. Select URL in the Purge by field.
5. Enter the appropriate value(s) in the text box, using the format type shown in the example.
6. Perform any additional instructions to complete the form.
7. Review your entries.
8. Click Purge.
Purge everything
To maintain optimum site performance, we strongly recommend using single-file (by URL) purging instead of a complete cache purge.
Purging everything immediately clears all resources from your CDN cache in all Cloudflare data centers. Each new request for a purged resource returns to your origin server(s) to validate the resource. If Cloudflare can't validate the resource, it fetches the latest version from the origin server and replaces the cached version. When a site with heavy traffic contains a lot of assets, requests to your origin server(s) can increase substantially, and may result in slow site performance.
Purge everything through your Cloudflare dashboard
Reminder: We highly recommend performing single-file purges.
1. Login to Cloudflare and select the site with the resources to purge.
2. Click the Caching app.
3. In the Purge Cache pane, click Purge Everything.
A warning dialog appears.
4. If you agree, click Purge Everything.
Purge cached resources through the API
You can automate or script cache purges using these Cloudflare APIs:
Purge using Cache-Tags (Cloudflare Enterprise only)
Cache-tag purging makes multi-file purging easier. You can perform bulk purging by adding cache-tags to your assets (web pages, image files, and so on).
How Cache-Tags work
This is the general workflow for using cache-tags:
- Add tags to the Cache-Tag HTTP response header from your origin web server for your web content (pages, static assets, and so on).
- Ensure your web traffic is proxied through Cloudflare.
- Cloudflare associates the tags in the Cache-Tag HTTP header with the content being cached.
- Use specific cache-tags to purge your Cloudflare CDN cache of all content containing that cache-tag, through your dashboard or using our API.
- Cloudflare forces a cache miss on content with the purged cache-tag.
Add Cache-Tag HTTP response headers
You add cache-tags to your web content in Cache-Tag HTTP response headers. You use HTTP headers to allow the client and the server to pass additional information in requests or responses. HTTP headers consist of a specific case-insensitive name followed by a colon ( : ) and the valid value, for example, Cache-Tag:tag1,tag2,tag3. As shown, you use commas to separate the tags when you want to use multiple cache-tags.
When your content reaches our edge network, Cloudflare:
- Removes the Cache-Tag HTTP header before sending the response to your website visitor.
Your end users never see Cache-Tag HTTP headers on your Cloudflare-enabled website.
- Removes whitespaces from the header and any before and after cache-tag names:
tag1, tag2 and tag1,tag2 are considered the same.
- Removes all repeated and trailing commas before applying cache-tags:
tag1,,,tag2 and tag1,tag2 are considered the same.
A few things to remember:
- A single HTTP response can have more than one Cache-Tag HTTP header field.
- The minimum length of a cache-tag is 1 byte.
Individual tags don’t have a maximum length, but the aggregate Cache-Tag HTTP header cannot exceed 16 KB after the header field name, which is approximately 1000 unique tags. Length includes whitespace and commas, but not the header field name.
- For cache purges, the maximum length of cache-tags in an API call is 120 characters.
- The Cache-Tag HTTP header must accept all valid characters allowable in HTTP headers, as specified in RFC-5987.
- Spaces are not allowed in cache-tags.
- Case does not matter:
Tag1 and tag1 are considered the same.
Purging using cache-tags in your Cloudflare dashboard
1. Login to Cloudflare and select the site with the resources to purge.
2. Click the Caching app.
3. In the Purge Cache pane, click Custom Purge.
The Custom Purge dialog box appears.
4. Select Tag in the Purge by section.
5. In the text box, enter your tags to use to purge the cached resources.
6. Click Purge.
Purging using cache-tags or hostnames via the Cloudflare API
Read our documentation on removing cached assets from your CDN using the Purging by cache-tag or hostname APIs. You can use up to 30 cache-tags in one API call and make up to 30,000 purge API calls in a 24-hour period.
Purge cache by prefix (Cloudflare Enterprise only)
Enterprise customers can purge their cache by URL prefix or path separators in their URL. For an example URL like https://www.example.com/foo/bar/baz/qux.jpg, valid purge requests include:
- www.example.com/
- www.example.com/foo/
- www.example.com/foo/bar/
- www.example.com/foo/bar/baz/
- www.example.com/foo/bar/baz/qux.jpg
Purging by prefix is useful for many occasions, such as:
- purge everything within a directory
- increase control over cached objects in a path
- simplify the number of purge calls sent
Purge by URL prefix is available via the Purge API:
curl -X POST "https://api.cloudflare.com/client/v4/zones/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/purge_cache" \ -H "X-Auth-Email: [email protected]" \ -H "X-Auth-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ -H "Content-Type: application/json" \ --data '{"prefixes":["www.example.com/foo","www.example.com/bar/baz"] }
Purge by URL prefix is also available via the UI:
- Log in to your Cloudflare account.
- Select the appropriate domain.
- Click on the Caching app in the dashboard.
- Click the Configuration tab.
- Scroll to the Purge Cache section
- Select Custom Purge and Prefix
- Follow the syntax instructions
- One prefix per line
- Maximum 30 prefixes per API call
There are several limitations regarding purge by prefix:
- Path separators are limited to 31 for a prefix (example.com/a/b/c/d/e/f/g/h/i/j/k/l/m…).
- Purge requests are limited to 30 prefixes per request.
- Purge rate-limits apply.
- URI query strings & fragments cannot purge by prefix:
- www.example.com/foo?a=b (query string)
- www.exmaple.com/foo#bar (fragment)