askvity

How Do I Delete a Key in Cloudflare KV?

Published in Cloudflare KV 4 mins read

To delete a key in Cloudflare KV (Key-Value Store), you can use the delete() method available in Workers or Pages Functions, the wrangler command-line tool, or the Cloudflare API.

Deleting a key removes both the key and its associated value from a KV namespace.

Methods for Deleting a Cloudflare KV Key

There are several ways to remove a key from your Cloudflare KV namespace, depending on your workflow:

1. Programmatically (Workers/Pages Functions)

The most common way to interact with KV from your application code is using the delete() method on your KV namespace binding.

  • Using delete(): Call the delete(key) method on your bound KV namespace object, passing the key you want to remove as a string argument.

    // Assuming 'MY_KV' is your KV namespace binding in your Worker/Pages function
    await MY_KV.delete('my-key-to-delete');
  • Success on Non-Existing Keys: As noted in the documentation, calling delete() on a non-existing key is returned as a successful delete. This means you don't need to check if a key exists before attempting to delete it; the operation will complete successfully either way.

  • Asynchronous Operation: The delete() method is asynchronous, returning a Promise. You should await the result to ensure the operation has been initiated.

  • Propagation Time: 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.

2. Using the Wrangler CLI

The wrangler command-line interface is a convenient tool for managing Cloudflare Workers and Pages, including interacting with KV namespaces directly from your terminal.

  • Command: Use the wrangler kv key delete command.

    wrangler kv key delete <key-name> --namespace-id <your-namespace-id>
    • Replace <key-name> with the specific key you want to delete.
    • Replace <your-namespace-id> with the ID of your KV namespace. You can find the namespace ID in the Cloudflare dashboard or by running wrangler kv:namespace list.
  • Example: To delete a key named user:123 from a namespace with ID abc123def456ghi789jkl012mno345pqr:

    wrangler kv key delete user:123 --namespace-id abc123def456ghi789jkl012mno345pqr

3. Using the Cloudflare API

For scripting or integrating with other services, you can use the Cloudflare REST API to manage your KV namespaces and keys.

  • Endpoint: Use the DELETE method on the key-specific endpoint.

    DELETE /accounts/:account_id/storage/kv/namespaces/:namespace_id/values/:key_name
  • Replace :account_id with your Cloudflare account ID, :namespace_id with your KV namespace ID, and :key_name with the key you want to delete.

  • You will need to authenticate your API request using an API token or key.

Key Considerations

  • Eventual Consistency: Deleting a key, like writing or updating one, follows an eventually consistent model. While the change is recorded quickly, it might take a short period for the deletion to propagate across Cloudflare's global network.
  • No Error on Non-Existence: As mentioned, deleting a key that doesn't exist does not return an error, which simplifies deletion logic in many cases.

By utilizing one of these methods, you can effectively remove unwanted keys and their associated data from your Cloudflare KV namespaces.

Related Articles