Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 36 ยท CDN and Caching

Cloudflare Cache Purge, Cache Keys and Speed Settings

Control freshness in Cloudflare: purge by URL, hostname, prefix or Cache-Tag, design cache keys with query strings, and enable compression, HTTP/3 and Early Hints.

Caching is only useful if you can also un-cache. This lesson covers the two levers that decide which object a request refers to (the cache key) and how to remove objects before their TTL ends (purging), then finishes with the speed settings that sit around the cache.

What is a cache key?

The cache key is the identity of a cached object. Two requests with the same key share one stored copy. By default Cloudflare builds the key from:

  • the URL scheme (https),
  • the hostname,
  • the path,
  • the full query string, in the order received.

So /img/a.jpg?w=100 and /img/a.jpg?w=200 are two objects, and so are /a.jpg?x=1&y=2 and /a.jpg?y=2&x=1. That is often wasteful: tracking parameters such as utm_source or fbclid create thousands of copies of one page and wreck the hit ratio.

In a cache rule you can customize the key:

  • Ignore the query string completely, or include only selected parameters (w, page) or exclude noise (utm_*).
  • Add a header (like Accept-Language), a cookie (for A/B variants), the device type or a country to the key, to keep several versions of the same URL apart.
  • Sort query parameters for consistent keys (Cloudflare can normalize their order).

A tighter key means more hits; a looser key means more variants. Aim for the smallest key that still separates content that really differs.

JS Stand-in: build cache keys

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Without normalization the three URLs are three objects; with it they share one. Remember the reverse risk: ignoring a parameter that does change content (like page) shows the wrong page to visitors.

Purging

When you publish a change, you need to remove old copies right away. In Caching > Configuration > Purge Cache you can choose:

Method What it removes Typical use
Purge Everything The whole zone's cache Big redesign, emergencies
Purge by URL Exact URLs (include the scheme and query string used in the key) A single updated page or image
Purge by Hostname All cached content for given hostnames One subdomain
Purge by Prefix Everything under a path like example.com/blog/ A section
Purge by Cache-Tag All responses carrying a tag A product, author or category across many URLs

Which methods exist on which plan has changed over time, so check the current documentation; URL purge and purge everything are available on every plan.

Tags

If your origin adds a Cache-Tag: product-42,category-shoes response header, Cloudflare remembers the tags with the stored object. Later one API call removes everything with product-42: the product page, the listing page that shows it and the JSON the app uses. This is the cleanest approach for dynamic sites, because your application knows which objects are related, and the CDN does not have to.

Purging by API

Terminal
# Purge two exact URLs
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
  -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  -H "Content-Type: application/json" \
  --data '{"files":["https://example.com/logo.png","https://example.com/blog/"]}'

# Purge by tag
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
  -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  -H "Content-Type: application/json" \
  --data '{"tags":["product-42"]}'

The token needs the permission Zone: Cache Purge. Call it from your deployment pipeline or CMS "publish" hook. Purges apply worldwide within seconds, but rate limits apply, so purge precisely instead of everything on every save.

JS Stand-in: purge by URL, prefix and tag

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that one tag removed two different URLs, something URL purging could not do without you knowing both addresses.

Speed settings around the cache

A few other switches in Speed and Caching affect delivery:

  • Compression. Cloudflare compresses text responses (HTML, CSS, JS, JSON, SVG) with Brotli or gzip depending on what the browser accepts, and newer encodings such as zstd where available. Already compressed files (JPEG, MP4, ZIP) are not recompressed.
  • HTTP/2 and HTTP/3. Both are on by default toward browsers, with HTTP/3 (QUIC) helping on lossy mobile networks.
  • Early Hints. The edge can send 103 Early Hints with Link: <style.css>; rel=preload before the full page arrives, so the browser starts downloading assets sooner.
  • Image optimization (Polish, Mirage and Image Resizing on suitable plans) converts to modern formats like WebP/AVIF and resizes for devices.
  • Rocket Loader and similar JavaScript-rewriting features can speed or break pages: test before enabling and prefer fixing the page itself.
  • Auto Minify was retired; minify in your build step.
  • Always Online and similar fallbacks serve archived copies when the origin is down; stale-if-error is more precise.

Treat each automatic optimization as a hypothesis: enable one, measure with real page tests, and keep it only if it helps.

Practical checklist

  1. Fingerprint static files and give them year-long TTLs.
  2. Strip tracking parameters from the cache key.
  3. Tag dynamic content from the origin and purge by tag after edits.
  4. Purge programmatically; reserve "purge everything" for emergencies.
  5. After a purge, verify with curl -sI that the first request is a MISS.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which request parts form Cloudflare's default cache key?

    By default scheme, hostname, path and the full query string identify the object.
  2. 2. Why remove utm_source parameters from the cache key?

    Each distinct query string is a separate object, even if the content is the same.
  3. 3. What is the best way to purge all pages that show a changed product?

    Tags let the origin declare relationships, and one purge call removes all related objects.
  4. 4. Which API token permission does a purge call need?

    Cache Purge is the permission for purge requests.

Summary

  • The cache key (scheme, host, path, query by default) defines identity; customize it to ignore noise or separate real variants.
  • Purge by URL, hostname, prefix, tag or everything; tags are the most precise for dynamic sites.
  • Automate purges with the API and a scoped token.
  • Compression, HTTP/3 and Early Hints are cheap wins; measure each optimization.

Next: SSL/TLS encryption modes