Skip to main content

Overview

Workers KV is a global, low-latency key-value data store. It supports exceptionally high read volumes with low latency, making it ideal for storing configuration data, cached content, and user preferences.

Managing Namespaces

Create a Namespace

Create a new KV namespace with an optional preview namespace for development:
Options:
  • <namespace> - The name of the new namespace
  • --preview - Create a preview namespace for testing
  • --env <environment> - Target a specific environment
  • --update-config - Automatically add the namespace to your wrangler.json
  • --name <binding-name> - Custom binding name for the namespace
Example:
After creation, add the namespace to your configuration:
wrangler.json

List Namespaces

View all KV namespaces in your account:
Outputs JSON with namespace details including IDs and titles.

Rename a Namespace

Rename an existing KV namespace:
Options:
  • <old-name> - Current namespace name (positional)
  • --namespace-id <id> - Target by ID instead of name
  • --new-name <name> - New namespace name (required)
Example:

Delete a Namespace

Delete a namespace and all its data:
Options:
  • <namespace> - Namespace name to delete
  • --binding <name> - Target by binding name
  • --namespace-id <id> - Target by namespace ID
  • --preview - Delete a preview namespace
  • -y, --skip-confirmation - Skip the confirmation prompt
This action is irreversible and permanently deletes all data in the namespace.

Key-Value Operations

Write a Key-Value Pair

Store a value in a KV namespace:
Options:
  • <key> - The key to write to
  • <value> - The value to write (mutually exclusive with --path)
  • --path <file> - Read value from a file
  • --binding <name> - Binding name from your config
  • --namespace-id <id> - Namespace ID to use
  • --preview - Write to preview namespace
  • --ttl <seconds> - Time-to-live in seconds
  • --expiration <timestamp> - UNIX timestamp for expiration
  • --metadata <json> - Arbitrary JSON metadata
  • --local - Use local storage (for dev)
  • --persist-to <dir> - Directory for local persistence
Examples:

Bulk Operations

Bulk Put

Upload multiple key-value pairs from a JSON file:
JSON Format:
Fields:
  • key - Key name (required)
  • value - Value to store (required)
  • expiration - UNIX timestamp for expiration
  • expiration_ttl - Seconds until expiration
  • metadata - JSON object for metadata
  • base64 - Whether value is base64 encoded

Bulk Get

Retrieve multiple values by keys (Open Beta):
Input Format:
Or with objects:

Bulk Delete

Delete multiple keys from a namespace:
Options:
  • -f, --force - Skip confirmation prompt
Input Format:

Local Development

Using Local Storage

Test KV operations locally without affecting production data:
Local mode stores data in .wrangler/state/v3/kv/ by default.

Worker Integration

Access KV from your Worker code:

Best Practices

Key Naming

  • Use prefixes for organization: user:123, config:theme
  • Keep keys under 512 bytes
  • Use consistent naming conventions
  • Avoid special characters when possible

Value Limits

  • Maximum value size: 25 MiB
  • Maximum metadata size: 1 KiB
  • Use expiration for temporary data
  • Consider compression for large values

Performance

  • KV is optimized for high read volumes
  • Writes propagate globally (eventual consistency)
  • Use list() pagination for large datasets
  • Cache frequently accessed data

Development

  • Use preview namespaces for testing
  • Test with --local flag during development
  • Use --persist-to for consistent local state
  • Version your data with metadata

Error Handling

Common errors and solutions:
Ensure the namespace exists and you’re using the correct binding name or namespace ID:
The specified key doesn’t exist in the namespace. Check the key name and use kv key list to verify.
Metadata must be valid JSON. Use proper escaping:
Values must be under 25 MiB. Consider:
  • Compressing the data
  • Splitting into multiple keys
  • Using R2 for larger objects