Use Cache Rules to customize cache settings on Cloudflare. Cache Rules allows you to make adjustments to what is eligible to cache, how long it should be cached and where, as well as trigger specific interactions with Cloudflare's cache and other Rules products for matching requests.
Cache Rules can be created in the dashboard, via API or Terraform.
Cloudflare provides you with rules templates for common use cases.
-
In the Cloudflare dashboard, go to the Rules Overview page.
Go to Overview ↗ -
Select Templates, and then select one of the available templates.
You can also refer to the Examples gallery in the developer docs.
The following table describes Cache Rules availability per plan.
| Free | Pro | Business | Enterprise | |
|---|---|---|---|---|
Availability | Yes | Yes | Yes | Yes |
Number of rules | 10 | 25 | 50 | 300 |
When a Cache Rule sets a custom cache key, the resulting cache entry is indexed by that key rather than the request URL alone. Depending on what the custom cache key includes, this may affect single-file purge:
- Custom cache keys that only change how the query string is handled (for example, ignoring the query string) generally work with dashboard single-file purge.
- Custom cache keys that include headers, cookies, or other request properties will prevent dashboard single-file purge from working, because the dashboard cannot send those values in a purge request.
- Even without Cache Rules, Cloudflare's default cache key includes certain request headers. Dashboard single-file purge may not work for resources cached with those headers present.
To purge resources that cannot be cleared via dashboard single-file purge, you have the following options:
- Use the API to purge by URL, including all headers, cookies, and query strings that are part of your custom cache key. If any header or cookie is missing from the purge request, it is treated as an empty value in the cache key.
- Purge by host, which clears all resources for a hostname and is not affected by custom cache keys.
- Purge by prefix, which purges all resources under a URL path and is not affected by custom cache keys.
- Purge by tag, which is not affected by custom cache keys.
- Purge everything, which clears all cached resources for the zone.
For more information, refer to Cache keys and Purge cache key resources.
When troubleshooting Cache Rules, use Cloudflare Trace to determine if a rule is triggering for a specific URL.