Skip to content

Delete key-value pairs

To delete a key-value pair, call the delete() method of the KV binding on any KV namespace you have bound to your Worker code:

env.NAMESPACE.delete(key);

Example

An example of deleting a key-value pair from within a Worker:

export default {
async fetch(request, env, ctx) {
try {
await env.NAMESPACE.delete("first-key");
return new Response("Successful delete", {
status: 200
});
}
catch (e)
{
return new Response(e.message, {status: 500});
}
},
};

Reference

The following method is provided to delete from KV:

delete() method

To delete a key-value pair, call the delete() method of the KV binding on any KV namespace you have bound to your Worker code:

env.NAMESPACE.delete(key);

Parameters

  • key: string
    • The key to associate with the value.

Response

  • response: Promise<void>
    • A Promise that resolves if the delete is successful.

This method returns a promise that you should await on to verify successful deletion. Calling delete() on a non-existing key is returned as a successful delete.

Calling the delete() method will remove the key and value from your KV namespace. As with any operations, it may take some time for the key to be deleted from various points in the Cloudflare global network.

Guidance

Delete data in bulk

Delete more than one key-value pair at a time with Wrangler or via the API.

The bulk API can accept up to 10,000 KV pairs at once. Bulk writes are not supported using the KV binding.

Other methods to access KV

You can also delete key-value pairs from the command line with Wrangler or with the API.