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:<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
wrangler.json
List Namespaces
View all KV namespaces in your account:Rename a Namespace
Rename an existing KV namespace:<old-name>- Current namespace name (positional)--namespace-id <id>- Target by ID instead of name--new-name <name>- New namespace name (required)
Delete a Namespace
Delete a namespace and all its data:<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
Key-Value Operations
- Put
- Get
- List
- Delete
Write a Key-Value Pair
Store a value in a KV namespace:<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
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 expirationexpiration_ttl- Seconds until expirationmetadata- JSON object for metadatabase64- 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
Local Development
Using Local Storage
Test KV operations locally without affecting production data:.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
--localflag during development - Use
--persist-tofor consistent local state - Version your data with metadata
Error Handling
Common errors and solutions:Namespace not found
Namespace not found
Ensure the namespace exists and you’re using the correct binding name or namespace ID:
Key not found
Key not found
The specified key doesn’t exist in the namespace. Check the key name and use
kv key list to verify.Invalid metadata JSON
Invalid metadata JSON
Metadata must be valid JSON. Use proper escaping:
Value too large
Value too large
Values must be under 25 MiB. Consider:
- Compressing the data
- Splitting into multiple keys
- Using R2 for larger objects
Related Commands
- R2 Storage - Object storage for large files
- D1 Database - Serverless SQL database
- Durable Objects - Stateful coordination