skip to content

After a Cloudflare purge by URL of a corrected match page, some visitors still get the old score; why, and how do you clear every copy?

level: seniorimportance: should knowfreq 45%

answer

  1. one URL, one cache key
  2. query strings make new objects
  3. headers the dashboard cannot send
  4. GET-only rules miss PURGE
  5. prefix or tag clears the rest

basics

~20 s

A URL purge removes one cache key. Copies under other keys, such as query-string variants or custom keys built from device type, headers or cookies, survive. Purge by prefix or tag, or send each variant's headers in an API purge.

solid answer

~40 s

Purge by URL deletes the object for **one cache key**, and the default key includes the full query string, so `/matches/8812?utm_source=app` is a separate cached copy the purge never touched. A Cache Rule that adds headers, cookies, device type or country to the key creates more variants; the dashboard cannot send those values, so you purge them through the API with a `headers` object per URL, where a missing header counts as an empty value. A Cache Rule matching only `http.request.method eq "GET"` does not match the purge, so add `PURGE` to it. Check `cf-cache-status` and `Age` on each variant, then clear them together with **purge by prefix** `scores.example.com/matches/8812`, which covers every query string, or with **purge by tag**.

code

json · 13 lines
json
{
  "files": [
    "https://scores.example.com/matches/8812",
    {
      "url": "https://scores.example.com/matches/8812",
      "headers": { "CF-Device-Type": "mobile" }
    },
    {
      "url": "https://scores.example.com/matches/8812",
      "headers": { "CF-Device-Type": "tablet" }
    }
  ]
}

go deeper

for a junior

Recall that one URL purge removes one cached object, and that a different query string is a different object.

for a middle

Explain how the default cache key and custom key parts create variants, and how the API's headers object purges a header or cookie variant.

for a senior

Diagnose with cf-cache-status and Age per variant, fix GET-only rule expressions with PURGE, and choose prefix or tag purges so every copy goes in one operation.

for a principal

Push the team toward cache keys without needless variants and a tag scheme, so a correction never depends on someone guessing which variants exist.

## The symptom An editor corrects match 8812's score and purges `https://scores.example.com/matches/8812`. Most visitors see the new score, yet a share of them, often app users, people arriving from a newsletter or phone visitors, still see the old one. The purge did exactly what it was asked: it removed **one cached object**. The visitors who still see the old score are being served **other objects** that the purge never named. ## Why one URL is not one object Cloudflare finds a cached response by its **cache key**. By default the key is built from the **full URL** (scheme, host, path and the **full query string**) plus a few request headers such as `Origin`, and a Cache Rule can add more parts. A single-file purge deletes the object whose key matches the URL you sent. So: - **Query-string variants** are separate keys. `/matches/8812?utm_source=app`, `/matches/8812?ref=newsletter` and `/matches/8812` are three objects unless a Cache Rule ignores the query string. Purging the bare URL clears only the bare one. - **Custom cache keys** that include request headers, cookies, device type or country store one object per value (device type is on every plan; header and cookie parts are Enterprise options). The dashboard's URL purge cannot send those values, so it does not remove them. Enterprise zones can set device type and country under **Advanced (Purging with custom cache keys)**; everything else goes through the API, where each `files` entry carries a `headers` object, for example `CF-Device-Type: mobile`, `CF-IPCountry: ES` or `accept-language: zh-CN`. A header left out of the purge is treated as an **empty value** in the key, so that entry targets only the variant whose key had no value for it. - **Cache Rules that match only `GET`**, such as `http.host eq "scores.example.com" and http.request.method eq "GET"`, do not match during the purge. Add the purge method: `(http.request.method eq "GET" or http.request.method eq "PURGE")`. Rules that match a field that cannot be evaluated at purge time, such as `cf.bot_management.score`, need purge by prefix, tag or everything instead. - **Objects cached with special request headers**: the dashboard purge does not clear objects cached with an `Origin` header or headers such as `X-Forwarded-Host`, `X-Host`, `X-Forwarded-Scheme`, `X-Original-URL`, `X-Rewrite-URL` or `Forwarded`. The API purge clears them when its `headers` object carries the matching values. - **Redirects and rewrites**: purging a URL that answers `301` removes the cached redirect, not the destination page; under a Transform Rule that rewrites the path, single-file purge needs the visitor-facing URL. ## Diagnosing which copy is stale 1. Collect the exact URLs that show the old score: full query string, the device, the country. 2. Replay each with `curl -sI`, adding the headers that the cache key uses, and read `cf-cache-status` and `Age`. Cloudflare sets `Age` on `HIT`, `STALE` and `UPDATING`, so an old `HIT` with a large `Age` is a copy cached before your purge. 3. Compare with the Cache Rules that apply to the path: which parts does the cache key include, and does the rule's expression match a purge? 4. Rule out what is not Cloudflare: a browser holding its own copy, or an origin replica still serving the old score. ## Clearing every copy | Method | What it clears | Use it when | | --- | --- | --- | | Purge by URL with `headers` (API) | One key per entry, including a header or cookie variant | You know the variants exactly | | Purge by prefix `scores.example.com/matches/8812` | Every object under that path, **whatever the query string** | Query-string variants | | Purge by tag `match-8812` | Every object tagged at the origin | Page, feed and tables together | | Purge by hostname | Everything on one host | A whole host is wrong | | Purge everything | The whole zone | Last resort, at the cost of an origin spike | Prefix purge has its own rules: the prefix is hostname plus path without a scheme, it may not contain a query string or fragment, it allows at most 31 path separators, and under a Transform Rule it takes the **rewritten** (origin) path, the opposite of single-file purge. Purge by tag, by hostname and purge everything are not affected by custom cache keys at all, which is why a tag scheme is the durable fix for pages that have many variants. ## Making the next correction clean - Tag match pages and feeds at the origin, and let the CMS purge by tag on every correction. - Keep tracking parameters out of the cache key with a Cache Rule if they do not change the page (ignoring the whole query string works on every plan; choosing parameters is an Enterprise option), which also raises the hit ratio. - Add `PURGE` to any Cache Rule expression that tests the request method. - Keep browser lifetimes on score pages short, because no purge reaches a visitor's browser.

  • Why does purge by prefix clear the query-string variants when purge by URL does not?
    Purge by prefix matches every cached object whose URL starts with the given hostname and path, whatever query string follows. That is also why a prefix itself may not contain a query string or fragment. Purge by URL instead matches one exact key, and the default key includes the full query string.
  • The page is behind a Worker that fetches it from another hostname. Which URL do you purge?
    The URL in the Worker's `fetch()` request, because that is what Cloudflare cached, not the visitor's URL. If that fetched hostname is itself proxied, the purge has to be sent to the zone that owns it.

  • Purge by URL removes every query-string variant of the page.
  • The dashboard URL purge clears variants keyed on headers or cookies.
  • A Cache Rule's expression has no effect on whether a purge applies.
  • Purging a URL that redirects also clears the page it redirects to.
  • Purge by tag misses objects that use a custom cache key.