Skip to content

Cloudflare cache purge: URL, tag, hostname, prefix, or everything

A purge-everything button is the wrong reflex. Cloudflare offers five purge surfaces — URL, prefix, hostname, tag, and everything — each with different blast radius and cold-start cost. Pick the smallest one that does the job and pre-warm afterward.

A content editor changes the homepage hero and asks marketing to “clear the Cloudflare cache”. The operator hits Purge Everything, the homepage reloads correctly, and ten minutes later the on-call gets paged because every page on the site is suddenly slow. The cache is cold across every POP, the origin is now serving traffic it has not seen in weeks, and downstream services start to wobble. Purge Everything is the right tool exactly twice a year. The other 363 days, a targeted purge does the job without the cold-start tax. This article walks the five purge surfaces, when to use each, the API shape, and a pre-warm pattern that takes the sting out of large purges.

How to verify

Pick a URL you intend to purge, capture its current cf-cache-status, then purge and recheck:

URL="https://www.example.com/styles/app.css"

# Before
curl -sI "$URL" | grep -iE '^(cf-cache-status|age|etag)'

# Purge a single URL via API
curl -sX POST \
  -H "Authorization: Bearer $CF_API_TOKEN" \
  -H "Content-Type: application/json" \
  "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
  --data "{\"files\":[\"$URL\"]}" | jq .

# After (should be MISS, then HIT on a second request)
curl -sI "$URL" | grep -iE '^(cf-cache-status|age)'
curl -sI "$URL" | grep -iE '^(cf-cache-status|age)'

The first curl after purge will be MISS or EXPIRED; the second should be HIT from the same POP. If you do not see this pattern, the URL was never cached to begin with — check the Cache Rules for that path.

What’s happening

Cloudflare exposes five distinct purge primitives, each tied to a different way of identifying objects in the edge cache.

Purge by URL removes specific URLs you list. This is the everyday tool. The API accepts up to 30 entries per request and you can issue many requests in parallel; for serious bulk work a single API call accepts up to 30 URLs. Each entry can include the protocol and host. If the same URL is cached under multiple cache-key variants (different Accept-Language headers, different device types), the URL purge clears all variants for that URL.

Purge by hostname removes everything cached for a given host on the zone. Useful when you have multiple hostnames on one zone (api.example.com, www.example.com, assets.example.com) and only one needs flushing. Enterprise plan and above.

Purge by prefix removes everything whose URL starts with a given path. purge by prefix: www.example.com/blog/ clears every cached /blog/... URL on that host. Useful for content sections that rev together. Enterprise plan and above.

Purge by Cache-Tag is the most precise tool when you have it. The origin attaches a Cache-Tag response header listing one or more arbitrary tags (e.g. Cache-Tag: product-1234, category-shoes, layout-v3). A tag purge removes every cached object that carries that tag, regardless of URL. This is the right pattern for content management systems — tag every cached page with post-<id>, then purge by post-<id> when an editor saves. Enterprise plan and above.

Purge everything clears the entire zone. Every URL on every host on the zone is removed from cache, globally, in seconds. The next request to any URL is a MISS, fetched from origin. Reserve this for: a security incident where you suspect cache poisoning, a TLS-mode change that should not serve old objects, or initial cutover of a new origin.

smallest blast radius → largestPurge URL1–30 URLsall plansPurge TagCache-Tag headerEnterprisePurge Prefix/blog/, /api/v1/EnterprisePurge Hostnameapi.example.comEnterprisePurgeEverythinglast resortOrigin offload after a purgeURL: ~1 object missed per POP Tag: 10s–1000s objects per tagPrefix: a section Hostname: every URL on host Everything: zone-wide cold cachePre-warm proportionally to blast radius.
Pick the smallest purge that does the job; reserve “purge everything” for cutovers and incidents.

The procedure

  1. Default to purge by URL for editorial changes. When a single page is updated, list the canonical URL(s) explicitly. Include both protocol and host. List both the https:// and any redirect sources if the redirect itself is cached.

    curl -sX POST \
      -H "Authorization: Bearer $CF_API_TOKEN" \
      -H "Content-Type: application/json" \
      "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
      --data '{
        "files": [
          "https://www.example.com/",
          "https://www.example.com/sitemap.xml"
        ]
      }'
  2. Adopt Cache-Tag from day one if you are on Enterprise. Have the origin emit a Cache-Tag header on cached responses. The header is a comma-separated list of arbitrary strings. A blog post renders with Cache-Tag: post-9381, author-erin, layout-v4. The category index emits Cache-Tag: category-shoes, layout-v4. When the editor saves post 9381, the CMS calls:

    curl -sX POST \
      -H "Authorization: Bearer $CF_API_TOKEN" \
      -H "Content-Type: application/json" \
      "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
      --data '{"tags":["post-9381","category-shoes"]}'

    Every cached page tagged with either of those values is dropped, regardless of URL. The blast radius is exactly the content that changed. The header itself is stripped from responses before they reach the user.

  3. Use prefix purges for section-wide rev events. A documentation site re-publishes the /docs/ tree on every release. Instead of enumerating thousands of URLs, issue one prefix purge:

    curl -sX POST \
      -H "Authorization: Bearer $CF_API_TOKEN" \
      -H "Content-Type: application/json" \
      "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
      --data '{"prefixes":["www.example.com/docs/"]}'

    Prefix matches are exact-string-prefix; www.example.com/docs does not match www.example.com/docs-v2/. Watch the trailing slash.

  4. Use hostname purges when an origin changes. Migrating api.example.com to a new backend? Purge that hostname so no stale responses from the old origin survive. Other hostnames on the zone keep their warm caches.

  5. Reserve “Everything” for cutovers and incidents. If the next response from origin would be materially different across the whole zone (TLS mode change, full content management system swap, suspected poisoned cache), Purge Everything is correct. Otherwise it is the wrong tool.

  6. Pre-warm after large purges. A zone-wide cold cache means the next request to every URL hits origin. For sites with concentrated traffic on a known set of URLs, run a pre-warm script immediately after the purge:

    # pre-warm top-200 URLs from CSV against a chosen POP via cf-connecting-ip route
    while IFS= read -r url; do
      curl -s -o /dev/null -w "%{http_code} %{time_total}s $url\n" "$url"
    done < top-200-urls.csv

    For more even coverage, run the pre-warm from multiple regions so different POPs warm in parallel — a single warmer only fills the cache at one or two POPs.

  7. Log every purge. Whatever runs your purges (CMS hook, deploy pipeline, ops script) should write to a log: who, when, what was purged, the API response. Cache misbehaviour debugging starts with “what was the last thing we purged?” — and if there is no log, you guess.

Common pitfalls

  • Purging by URL only clears the exact URL you list — https://example.com/page does not clear https://www.example.com/page (different host) or http://example.com/page (different scheme); list every variant.
  • The Cache-Tag response header is stripped from the response before it reaches users, but if your CDN-in-front-of-Cloudflare or downstream proxy logs raw responses, the tags appear there — treat them as non-secret.
  • “Purge by hostname” still respects the zone — you cannot purge a hostname that lives on a different Cloudflare zone from the API call’s $ZONE_ID.
  • Issuing a Purge Everything mid-incident without a pre-warm plan can take a slow origin down — measure your origin’s headroom against a cold cache before pulling the trigger.
  • API tokens for purges should be scoped to the Zone.Cache Purge permission only; do not reuse a full Account API token in a CMS plugin.

In the engagements we run, the purge surface is wired into the Cache Rules plan from day one — the origin emits Cache-Tag, the CMS knows which tags to send, and Purge Everything is a button that has been pressed twice this year, both times intentionally. Stack Harbor operates this kind of cache discipline under Managed Operations.