Skip to main content
Version: 3.27.2

SimpleCache.purge

purge the entry associated with the key key from the cache.

Syntax

purge(key, options)

Parameters

  • key : string

    • The key to purge from within the cache.
  • options : object

    • scope : string
      • : Where to purge the content from.
      • Possible values are:
        • "global" - This will remove the content from all of Fastly.
        • "pop" - This will remove the content from the POP that contains the currently executing instance.

Return value

Returns undefined.

Exceptions

  • TypeError
    • If the provided key:
      • Is an empty string
      • Cannot be coerced to a string
      • Is longer than 8135 characters

Examples

In this example, when a request contains a purge querystring parameter, we purge the an entry from the cache.

/// <reference types="@fastly/js-compute" />

import { SimpleCache } from 'fastly:cache';

addEventListener('fetch', event => event.respondWith(app(event)));

async function app(event) {
const url = new URL(event.request.url);
const path = url.pathname;
if (url.searchParams.has('purge')) {
SimpleCache.purge(path, { scope: "global" });
return new Response(page, { status: 204 });
}

let page = SimpleCache.getOrSet(path, async () => {
return {
value: await render(path),
// Store the page in the cache for 1 minute.
ttl: 60
}
});
return new Response(page, {
headers: {
'content-type': 'text/plain;charset=UTF-8'
}
});
}

async function render(path) {
// expensive/slow function which constructs and returns the contents for a given path
await new Promise(resolve => setTimeout(resolve, 10_000));
return path;
}