Delete and recreate a namespace as a way to delete all keys at once?

I see that the API allows you to delete a namespace. It also allows you to create a namespace. Would this be a feasible way to delete all keys in a namespace? I would need to be able to recreate a namespace with the same name after removing it and give my workers access to it again.

Yes, it’s possible but you may have unexpected side-affects such as the Worker needing to reload when you remove and re-add your bindings.

You can use the Cloudflare API to:

  1. get your Workers metadata which includes the bindings;

  2. execute the API request to delete your current KV namespace which will remove the binding;

  3. execute the API request to create your new KV namespace;

  4. and finally, PUT your Worker update request with the amended bindings to change the old KV namespace id to the new one.

This will essentially remove, create, attach a new KV namespace on your Worker.

Again I’ll stress that this is not recommended. It’s better to implement an alternative logic into your application such as virtual namespace in your application and a garbage collection routine as just one example, if you can elaborate more details about your application then I can give you a more specific solution.

1 Like


I just saw you posted an answer in my other topic about this that was more specifically just about the undocumented API call. Thank you.

Looking at your list I think maybe there needs to be one more step at the beginning of downloading the scripts so that it can be attached again during step 4 when it is reuploaded seeing as that is a necessary part of editing the bindings. When you said that the scripts need to be reloaded I didn’t realize you meant re-uploaded. But that’s totally doable.

No problem. Glad my solution could answer your question, but I will say again that I don’t recommend that method of truncation of your KV namespace.

Honestly there is a lot of issues with the Cloudflare API documentation and I don’t know if its undocumented because they are yet to add the documentation or if they’ve forgot to add it, or if its undocumented because its only an intermediary solution :tipping_hand_man:. Without confirmation from Cloudflare I don’t know if you can rely on that method shown on the other thread. A Cloudflare staff member is very hard get in contact with and even when you can speak to somebody, its rare they’ll even know what your talking about.

Theres also a few bugs in the platform that are just a disaster waiting to happen but theres too much friction in being able to communicate to anybody and make them understand it.