Purge Cached Content

### Purge All Cached Content Removes ALL files from Cloudflare's cache.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Purge Cached Content
8
 * ### Purge All Cached Content
9
Removes ALL files from Cloudflare's cache.
10
 */
11
export async function main(
12
  auth: Cloudflare,
13
  identifier: string,
14
  body:
15
    | (
16
        | { tags?: string[]; [k: string]: unknown }
17
        | { hosts?: string[]; [k: string]: unknown }
18
        | { prefixes?: string[]; [k: string]: unknown }
19
      )
20
    | { purge_everything?: boolean; [k: string]: unknown }
21
    | {
22
        files?: (
23
          | string
24
          | {
25
              headers?: { [k: string]: unknown };
26
              url?: string;
27
              [k: string]: unknown;
28
            }
29
        )[];
30
        [k: string]: unknown;
31
      }
32
) {
33
  const url = new URL(
34
    `https://api.cloudflare.com/client/v4/zones/${identifier}/purge_cache`
35
  );
36

37
  const response = await fetch(url, {
38
    method: "POST",
39
    headers: {
40
      "X-AUTH-EMAIL": auth.email,
41
      "X-AUTH-KEY": auth.key,
42
      "Content-Type": "application/json",
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: JSON.stringify(body),
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53