0

Delete multiple key-value pairs

by
Published Nov 16, 2023

Remove multiple KV pairs from the namespace. Body should be an array of up to 10,000 keys to be removed.

Script cloudflare Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Delete multiple key-value pairs
8
 * Remove multiple KV pairs from the namespace. Body should be an array of up to 10,000 keys to be removed.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  namespace_identifier: string,
13
  account_identifier: string,
14
  body: string[]
15
) {
16
  const url = new URL(
17
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/storage/kv/namespaces/${namespace_identifier}/bulk`
18
  );
19

20
  const response = await fetch(url, {
21
    method: "DELETE",
22
    headers: {
23
      "X-AUTH-EMAIL": auth.email,
24
      "X-AUTH-KEY": auth.key,
25
      "Content-Type": "application/json",
26
      Authorization: "Bearer " + auth.token,
27
    },
28
    body: JSON.stringify(body),
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36