Delete Worker (Workers for Platforms)

Delete a worker from a Workers for Platforms namespace. This call has no response body on a successful delete.

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
 * Delete Worker (Workers for Platforms)
8
 * Delete a worker from a Workers for Platforms namespace. This call has no response body on a successful delete.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  dispatch_namespace: string,
14
  script_name: string,
15
  force: string | undefined
16
) {
17
  const url = new URL(
18
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/workers/dispatch/namespaces/${dispatch_namespace}/scripts/${script_name}`
19
  );
20
  for (const [k, v] of [["force", force]]) {
21
    if (v !== undefined && v !== "") {
22
      url.searchParams.append(k, v);
23
    }
24
  }
25
  const response = await fetch(url, {
26
    method: "DELETE",
27
    headers: {
28
      "X-AUTH-EMAIL": auth.email,
29
      "X-AUTH-KEY": auth.key,
30
      "Content-Type": "application/json",
31
      Authorization: "Bearer " + auth.token,
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41