1. Opening Problem Statement
Meet Sarah, a developer managing multiple Cloudflare Workers projects which rely on Cloudflare’s Key-Value (KV) storage for configuration and data caching. Every week, Sarah spends hours manually creating new namespaces, adding key-value pairs, deleting outdated data, and renaming KV namespaces. This manual process leads to frequent errors such as deleting wrong keys or misnaming namespaces — costing her both time and project reliability.
This n8n workflow offers Sarah an efficient, automated solution to handle all interactions with Cloudflare KV storage via API calls, allowing her to focus on development while n8n performs error-free KV management.
2. What This Automation Does
This workflow provides a full suite of automated actions that interact with Cloudflare KV namespaces and their key-value pairs. When triggered, it:
- Creates new KV namespaces on Cloudflare automatically.
- Lists all existing KV namespaces under a Cloudflare account.
- Deletes specific keys or multiple keys within a namespace in bulk.
- Renames an existing KV namespace safely by searching and updating the name.
- Writes or updates multiple key-value pairs at once within a selected namespace.
- Reads values or metadata of keys stored in any namespace.
By automating these operations, this workflow can save hours per week that would otherwise be spent navigating the Cloudflare dashboard or writing scripts manually. It eliminates human error in API calls and streamlines complex KV management tasks into repeatable, maintainable automation steps.
3. Prerequisites ⚙️
- n8n account with access to create workflows.
- Cloudflare account with API token that has permissions to manage Workers KV storage namespaces and key-value pairs.
- Pre-configured Cloudflare API credential in n8n (used by HTTP Request nodes).
4. Step-by-Step Guide
Step 1: Manual Trigger Setup
Navigate to your n8n workflow editor. Add a Manual Trigger node to start the workflow manually. This allows you to test and execute each KV operation on demand.
You should see a simple trigger node named “Manual Trigger” ready to initiate the flow.
Common mistake: Forgetting to add this trigger will prevent the workflow from running.
Step 2: Define Your Cloudflare Account Identifier
Next, configure a Set node named “Account Path”. Enter your Cloudflare account’s identifier (Account ID) in the field named Account Path (account_identifier).
This value is crucial as all HTTP API requests use it to identify your specific Cloudflare account.
Example value: 65889d72a808df2e380018d87fffca5f
Expected outcome: This node’s output will be read by subsequent nodes to build correct API URLs.
Common mistake: Using an incorrect or expired account ID leads to authorization failures.
Step 3: Create a New KV Namespace
Add a Set node named “Set KV-NM Name (Create)” to specify the namespace title you want to create.
Then connect an HTTP Request node named “Create KV-NM” configured as follows:
Method: POST
URL: https://api.cloudflare.com/client/v4/accounts/{{ $('Account Path').json['Account Path (account_identifier)'] }}/storage/kv/namespaces
Body Parameters: { "title": "YourNewNamespaceName" }
Authentication: Use your predefined Cloudflare API credential
After execution, a new KV namespace with the given name will be created.
Visual: The API returns JSON with details including the new namespace ID.
Common error: Omitting the authorization credential or incorrect URL formatting.
Step 4: List Existing KV Namespaces
Add an HTTP Request node named “List KV-NMs” to perform a GET on:
https://api.cloudflare.com/client/v4/accounts/{{ $('Account Path').json['Account Path (account_identifier)'] }}/storage/kv/namespaces
Query Parameters: direction=asc, order=id, page=1, per_page=20
Authentication: Cloudflare API credential
This returns a list of all KV namespaces associated with your account.
Use this list to reference namespace IDs for further operations.
Common mistake: Not paginating results when you have many namespaces.
Step 5: Delete a Specific KV Namespace
Use a Set node to specify the namespace name to delete, named “Set KV-NM Name (Delete)”.
Then configure an HTTP Request node “Delete KV”:
Method: DELETE
URL: https://api.cloudflare.com/client/v4/accounts/{{ $('Account Path').json['Account Path (account_identifier)'] }}/storage/kv/namespaces/{{ namespaceId }}
Authentication: Cloudflare API credential
The namespace ID is dynamically found by searching the list of namespaces for the one matching your specified title.
Execution results in complete removal of the targeted namespace.
Common error: Attempting to delete namespaces that still have active keys without cleanup.
Step 6: Rename an Existing KV Namespace
In a Set node named “KV to Rename”, enter your existing namespace name and the new name.
Follow with an HTTP Request node “Delete KV1” using method PUT and body:
{ "title": "NewName" }
The URL includes the namespace ID found by searching for the previous name.
This updates the namespace’s title on Cloudflare.
Common mistake: Providing invalid names or missing authentication.
Step 7: Write Multiple Key-Value Pairs to a Namespace
Use a Set node to specify the namespace name, then use the HTTP Request node “Write KVs inside NM” configured as:
Method: PUT
URL: https://api.cloudflare.com/client/v4/accounts/{{ accountId }}/storage/kv/namespaces/{{ namespaceId }}/bulk
Body (JSON):
[
{ "key": "key1", "value": "Value1", "base64": false, "expiration": 1578435000, "expiration_ttl": 300 },
{ "key": "key2", "value": "Value2", "base64": false, "expiration": 1578435000, "expiration_ttl": 300 }
]
Authentication: Cloudflare API
This node writes or updates multiple keys in bulk efficiently.
Common mistake: Incorrect JSON formatting or keys that already exist without intended overwriting.
Step 8: Delete Multiple Keys in a Namespace
Specify the namespace and keys to delete in a Set node, then use HTTP Request node “Delete KVs inside NM” with method DELETE and a JSON array body listing keys:
[
"key1",
"key2",
"key3"
]
This deletes multiple keys in one API call.
Common error: Trying to delete keys not present raises errors; validate keys beforehand.
Step 9: Read the Value or Metadata of a Specific Key
Use separate HTTP Request nodes for reading the value and metadata of a key by specifying the namespace and key name:
Value URL: https://api.cloudflare.com/client/v4/accounts/{{ accountId }}/storage/kv/namespaces/{{ namespaceId }}/values/{{ keyName }}
Metadata URL: https://api.cloudflare.com/client/v4/accounts/{{ accountId }}/storage/kv/namespaces/{{ namespaceId }}/metadata/{{ keyName }}
Set appropriate HTTP method GET and check the JSON/text response.
Common mistake: Passing wrong key or namespace IDs leads to 404 errors.
5. Customizations ✏️
- Customize TTL for keys by adjusting the
expirationandexpiration_ttlfields in the bulk write node to fit your cache lifetime needs. - Extend pagination parameters in the list namespaces node to handle more than 20 namespaces if you have a large Cloudflare account.
- Add error handling nodes after HTTP requests to catch and log failed API calls automatically.
- Modify the metadata structure sent in the “Write V & MD of KV In NM” node to include additional custom attributes per your app requirements.
- Integrate this workflow with a webhook trigger or schedule trigger in n8n for automated or event-driven KV syncs.
6. Troubleshooting 🔧
Problem: “Authentication failed” errors on HTTP nodes.
Cause: Invalid or expired Cloudflare API token.
Solution: Recreate the API token with proper permissions and update it in n8n credentials settings.
Problem: “404 Not Found” when deleting or renaming namespaces.
Cause: The provided namespace name does not match any existing namespace.
Solution: Verify the namespace list by running the “List KV-NMs” node and use exact names.
Problem: Incorrect JSON body in bulk write causing API errors.
Cause: Syntax errors or missing required fields in the JSON array.
Solution: Use the exact payload format shown, test with a small set of keys first.
7. Pre-Production Checklist ✅
- Confirm your Cloudflare API token has full Workers KV permissions.
- Validate your account identifier and input namespace/key names carefully.
- Test each node independently using n8n’s manual execution.
- Backup existing KV namespaces if data retention is important before deletion.
- Verify network connectivity and that your n8n instance can reach Cloudflare APIs.
8. Deployment Guide
Once you have tested all KV operations manually, activate the workflow for scheduled or event-based triggers if desired.
Monitor the workflow execution via n8n’s execution logs to ensure all API calls succeed.
Use n8n’s error workflows or alerts to catch issues in production early.
9. Conclusion
With this comprehensive n8n workflow, you have automated the full lifecycle of managing Cloudflare KV storage: creating, listing, updating, renaming, deleting namespaces, and handling key-value pairs efficiently.
You saved hours weekly, eliminated manual command line or dashboard errors, and gained confidence in production KV management.
Next steps could be integrating this automation with your CI/CD pipeline or setting up real-time key updates triggered by external events.
Start exploring these advanced workflows to transform your Cloudflare KV management even further!