© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Cloudflare DevelopersCD
Cloudflare Developers•3y ago
x0nifo

purging fetch cache that uses a custom cache key

I'm using Workers to proxy requests to an origin server and cache the responses using the
fetch
fetch
cache. Essentially I'm doing something like:

// processes request to https://example.com/foo.json
export default {
  async fetch(request) {
    const url = new URL(request.url);
    const cacheKey = `https://${url.hostname}${url.pathname}`;

    const headers = new Headers(request.headers);
    headers.set("x-foo", url.host);

    # rewrite host so that we proxy to origin
    url.host = 'originserver.com'
    
    let response = await fetch(url.toString(), {
      cf: {
        cacheKey: cacheKey,
        cacheTtl: cacheTtlSec,
        cacheEverything: true,
        cacheTtlByStatus: {
          '100-199': 0,
          '200-299': cacheTtlSec,
          '300-599': 0,
        }
      },
      headers: headers,
    });
    response = new Response(response.body, response);
 
    return response;
  },
};
// processes request to https://example.com/foo.json
export default {
  async fetch(request) {
    const url = new URL(request.url);
    const cacheKey = `https://${url.hostname}${url.pathname}`;

    const headers = new Headers(request.headers);
    headers.set("x-foo", url.host);

    # rewrite host so that we proxy to origin
    url.host = 'originserver.com'
    
    let response = await fetch(url.toString(), {
      cf: {
        cacheKey: cacheKey,
        cacheTtl: cacheTtlSec,
        cacheEverything: true,
        cacheTtlByStatus: {
          '100-199': 0,
          '200-299': cacheTtlSec,
          '300-599': 0,
        }
      },
      headers: headers,
    });
    response = new Response(response.body, response);
 
    return response;
  },
};


Is there a way to purge this
fetch
fetch
cache via the API, using the Purge Cache endpoint? I've tried doing:

curl -s -X POST "https://api.cloudflare.com/client/v4/zones/xxx/purge_cache" -H "Authorization: Bearer xxx"  -H "Content-Type: application/json" -d '{"files":["https://originserver.com/foo.json"]}'
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/xxx/purge_cache" -H "Authorization: Bearer xxx"  -H "Content-Type: application/json" -d '{"files":["https://originserver.com/foo.json"]}'


but that didn't seem to have an effect in the subsequent requests to the cached resource (i.e. I always get
CF-Cache-Status: HIT
CF-Cache-Status: HIT
and
Last-Modified
Last-Modified
remains unchanged). I then tried different combinations like:

{"files":[{"url":"https://originserver.com/foo.json", "headers": {"x-foo": "example.com"}}]}
{"files":[{"url":"https://originserver.com/foo.json", "headers": {"x-foo": "example.com"}}]}


But that didn't work either. Any insights would be greatly appreciated.
Cloudflare Developers banner
Cloudflare DevelopersJoin
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news
85,042Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Purging cache in workers
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3mo ago
Fetch() cache behaviour
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago
Why purging cache by tags purges other caches?
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3mo ago
Implementing Worker Tag Cache and Purging by Tag
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
13mo ago