How to bulk delete KV in CloudFlare

Marija Trachtenberg
3 min readNov 28, 2022

The problem

Currently there is no way to ‘click’ and bulk delete a CloudFlare (CF) namespace (also called the Key-Value namespace or KV namespace).

While you can delete the namespace inside the CF user interface, this action is not meant for purging the namespace.

When you delete a namespace all CF workers that rely on it will break. Yes, while you could, in theory, re-create the namespace, you would then need to re-connect each CF worker to the namespace, which is extraordinarily tedious.

A peak inside a CloudFlare KV showing a table of example namespaces and their namespace IDs. Clicking on the three dots on any row will show you the option to delete that CloudFlare namespace.

The solution

The work-around to this problem is to use a combination of existing CloudFlare commands with terminal commands.

1. Install CloudFlare Wrangler

You need to install Cloudflare Wrangler (see here). The Wrangler is a way to interact with CloudFlare from your CLI (command line interface). Once you’ve done that, you can use the Wrangler commands to bulk delete up to 10,000 key-value pairs in any KV namespace (see the docs here).

2. Login to CloudFlare wrangler

In your terminal run this command which will prompt you to login to Cloudflare via the browser.

wrangler login

3. Create a JSON file

Once you’ve logged in, you’ll need to create an empty JSON file. I’ve named the file “PURGE”.

touch PURGE.json

4. Get all the keys from the KV namespace

Run the following command in the terminal.

wrangler kv:key list --namespace-id=${namespaceID} > PURGE.json

Replace the ${namespaceID} with the actual ID number (see below):

wrangler kv:key list --namespace-id=123456789 > PURGE.json

This command will populate the PURGE.json file with all of the keys in that particular namespace. The output will look like this.

[
{"name":"101"},
{"name":"102"},
{"name":"103"},
{"name":"104"},...
]

To see all other associated Wrangler commands, click here.

4.1 Get a subset of the namespace (optional)

If you want to get a subsection of the namespace to delete, just use the
-prefix flag.

wrangler kv:key list --namespace-id=123456789 --prefix="102" > PURGE.json

5. Bulk delete: issues

Unfortunately, the bulk delete command below will not work without a few formatting tweaks.

wrangler kv:bulk delete --namespace-id=123456789 PURGE.json

If you run that command without making any formatting changes to PURGE.json you will get this error from CloudFlare:

Error: Failed to decode JSON. Please make sure to follow the format, 
[{"key": "test_key", "value": "test_value"}, ...]

I’ve opened an issue for the CloudFlare team here.

6. Formatting JSON output

Instead, what you need to do is run a ‘find and replace’ inside the PURGE.json file:

  1. Find name and replace it with key
  2. Find: “},
    Replace it with: ”, “value”: “”},

Once you do that, you should see something like this:

[
{"name":"101", "value": ""},
{"name":"102", "value": ""},
{"name":"103", "value": ""},
{"name":"104", "value": ""},...
]

It doesn’t matter that the “value” portion is an empty string. This is okay and the CF wrangler command to bulk delete will work.

7. Bulk delete

Run the bulk delete command again.

wrangler kv:bulk delete --namespace-id=123456789 PURGE.json

It will ask you to confirm. This will delete all the keys in your cache (up to 10,000), with the namespace intact.

Conclusion

Let’s hope the CloudFlare team is able to resolve the issue I opened here: https://github.com/cloudflare/wrangler/issues/2330

Do you have any suggestions or other work-arounds? Perhaps you might be interested in submitting a pull request for the above issue.

--

--