Skip to content

Clear the cache when you deploy

Clear a site's page cache and its CDN after a deploy, and wait until they're cleared.

2 min read Updated

Clear a site's caches as the last step of a deploy, so visitors get the new version at once: the pages cached on its server, then its CDN when the CDN is on.

What you need

  • A token with sites:read (View sites) and sites:write (Manage sites).
  • The site's id.

1. Clear the pages cached on the server

POST /v1/sites/{siteId}/actions/purge-cache clears the pages RabbitPress Cache keeps for the site, when it's installed, and WordPress's object cache. It answers once the server has done it, within a minute and a half.

curl -X POST "https://api.rabbitflare.com/v1/sites/8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d/actions/purge-cache" \
  -H "Authorization: Bearer $RABBITFLARE_TOKEN"

2. Clear the CDN, when it's on

Read the site's CDN: when its status is active, clear it. Send no body to clear everything, or paths to clear only the files your deploy changed. A WordPress update gives changed plugin and theme files new addresses, so the CDN only needs clearing for files replaced under the same name.

curl -X POST "https://api.rabbitflare.com/v1/sites/8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d/cdn/actions/purge" \
  -H "Authorization: Bearer $RABBITFLARE_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
  "paths": [
    "/wp-content/themes/shop/style.css",
    "/wp-content/uploads/2026/11/*"
  ]
}'

3. Wait until the CDN has cleared

The answer is 202 with purging: true. Read the CDN until purging is false again: a minute or two. If purgedAt moved, the files are cleared; if it didn't, the clear failed, and you can send it again. Or let webhooks tell you (site.cdn_purged or site.cdn_purge_failed).

What can go wrong

CodeStatusWhat it meansWhat to do
server_unreachable 502 The server didn't answer, or didn't finish the request. Check that the server is running, then try again in a minute.
busy 409 Another change is still in progress on this server or site, or something else under way stops this one for now (a site backup running on the server, the server's backups being moved): message says what. Wait until it finishes, then try again. A change to the server or site itself is done when its pendingAction is null again; for anything else, message says how long to wait.
staging_busy 409 A staging operation is running on this site, or on a site of this server. Wait until it finishes (activeRun in GET /sites/{siteId}/staging is null again), or cancel it, then try again.
cdn_off 409 The site's CDN isn't on, and this needs it. Turn the CDN on first (POST /v1/sites/{siteId}/cdn/actions/enable) and wait until it's active.
rate_limited 429 Too many requests. scope says which limit: token, account, create, operation or network. Wait for Retry-After seconds, then go on more slowly.
not_found 404 The resource doesn't exist, or isn't this account's. Check the id. Ids of other accounts' resources answer the same way.

A site's CDN can be cleared only so many times an hour (the operation says how many): over that, the answer is 429, and the script waits as its Retry-After says. Any other refusal stops the script, with the API's code and message.

The whole script

Steps 1 to 3 in JavaScript (Node.js 18 or later; save it as an .mjs file). Put your site's id in SITE_ID, and run it after your deploy: it ends with an error when a clear didn't go through, or the CDN is still clearing after 10 minutes. To have a backup to go back to, back the site up before you deploy.

JavaScript
// Clears a site's caches after a deploy, so visitors get the new version at once: the pages cached on its server,
// then its CDN when the CDN is on. Run it as your deploy's last step.
const API = "https://api.rabbitflare.com/v1";
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

// Sends a request and gives back its data. Over a rate limit (429) it waits as long as Retry-After says, then sends
// the same request again; any other error is thrown with the API's code.
async function rabbitflare(method, path, body, headers = {}) {
  for (;;) {
    const res = await fetch(API + path, {
      method,
      headers: {
        Authorization: `Bearer ${process.env.RABBITFLARE_TOKEN}`,
        ...(body ? { "Content-Type": "application/json" } : {}),
        ...headers,
      },
      body: body ? JSON.stringify(body) : undefined,
    });
    if (res.status === 429) {
      await sleep((Number(res.headers.get("Retry-After")) || 1) * 1000);
      continue;
    }
    const answer = res.status === 204 ? null : await res.json().catch(() => null);
    if (!res.ok) throw Object.assign(new Error(answer?.error?.message ?? `HTTP ${res.status}`), { code: answer?.error?.code });
    return answer?.data ?? null;
  }
}

const SITE_ID = "8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d";

// 1. The pages cached on the server: RabbitPress Cache's, and WordPress's object cache (up to a minute and a half).
await rabbitflare("POST", `/sites/${SITE_ID}/actions/purge-cache`);
console.log("Cleared the pages cached on the server");

// 2. The CDN, when it's on for the site: everything in it (send { paths: [...] } to clear only some files).
let cdn = await rabbitflare("GET", `/sites/${SITE_ID}/cdn`);
if (cdn.status !== "active") {
  console.log("The CDN isn't on for this site: nothing more to clear");
} else {
  const before = cdn.purgedAt;
  cdn = await rabbitflare("POST", `/sites/${SITE_ID}/cdn/actions/purge`);

  // 3. Wait while the CDN clears (a minute or two). Done, purgedAt changes; if it didn't, the clear failed.
  const deadline = Date.now() + 10 * 60_000;
  while (cdn.purging) {
    if (Date.now() > deadline) throw new Error("The CDN is still clearing after 10 minutes");
    await sleep(10_000);
    cdn = await rabbitflare("GET", `/sites/${SITE_ID}/cdn`);
  }
  if (cdn.purgedAt === before) throw new Error("The CDN couldn't clear the files: run this again in a few minutes");
  console.log(`Cleared the CDN at ${cdn.purgedAt}`);
}

Still have a question?

Our team answers support tickets about the API, and other developers answer in the community.

Contact support

Popular

API docs