Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the output format of wrangler kv:key list --namespace-id command #2620

Open
Tracht opened this issue Nov 28, 2022 · 4 comments
Open
Labels
breaking change Change that will result in breaking existing behavior enhancement New feature or request

Comments

@Tracht
Copy link

Tracht commented Nov 28, 2022

馃挕 Feature request

In a nutshell: improve how kv list and kv bulk delete commands work together. I'd like to get a list of names in KV so that I can then run a wrangler command to perform a bulk delete. However the format of the kv list output is not what is accepted when running the kv bulk delete command.

Describe the feature

Current behaviour
When running the command wrangler kv:key list --namespace-id=someID the result will output an array of objects with the following:

[ 
{"name":"org_10"}, 
{"name":"org_11"},
{"name":"org_12"}, ... 
]

Desired feature
Can you add a flag to the command such that the results are formatted in a way that makes it easy to do a bulk delete, as required in the CloudFlare docs. For example:

Desired command
wrangler kv:key list --namespace-id=someID --deleteHelper

Desired output

[ 
{"key":"org_10", "value": ""}, 
{"key":"org_11, "value": ""}, 
{"key":"org_12", "value: ""}, ...
]

If I run the kv:bulk delete command using the output from wrangler kv:key list --namespace-id=someID I get the following error:

Error: Failed to decode JSON. Please make sure to follow the format, [{"key": "test_key", "value": "test_value"}, ...]

As you can see, the list and delete commands have different outputs which make it impossible to run the following:

wrangler kv:key list --namespace-id=${namespaceID} > PURGE.json

wrangler kv:bulk delete --namespace-id=${namespaceID} PURGE.json

Note
It is not necessary to return the actual values of each key. It is sufficient to just include "value":"" for the bulk delete command to work.

The other solution would be to update the kv:bulk delete command such that it accepts the current output of wrangler kv:key list --namespace-id=someID.

Describe the alternatives

Are there any alternatives to solving this problem? If so, what was your experience with them?

The way I am getting around this issue is quite tedious and manual. Currently I'm running the command:
wrangler kv:key list --namespace-id=${namespaceID} > PURGE.json

And then I am doing a find + replace command inside PURGE.json so that:
1- anywhere there is "name" it is replaced with "key"
2- and in order to insert the "value": "" bit that is required, I then search for:
"},
and replace it with
"", "value": ""},

And finally I am able to get the correct format required for bulk deletion.

@mrbbot mrbbot transferred this issue from cloudflare/wrangler-legacy Jan 25, 2023
@mrbbot mrbbot added the enhancement New feature or request label Jan 25, 2023
@mrbbot
Copy link
Contributor

mrbbot commented Jan 25, 2023

Hey! 馃憢 I've moved this issue over from the legacy wrangler repo to version 2's new repo, and added it to our backlog. 馃憤

@penalosa penalosa added the breaking change Change that will result in breaking existing behavior label Nov 15, 2023
@konsumer
Copy link

I would just like to see wrangler kv:key list and wrangler kv:bulk delete follow the same format.

Currently:

wrangler kv:key list --binding=LOG

outputs objects in array like

[
  {
    "name": "1705463215143/1705463215143250391365196660"
  }
]
wrangler kv:key list --binding=LOG > purge.json
wrangler kv:bulk delete --binding=LOG -f purge.json

outputs errors:

Expected an array of strings.
  The item at index 0 is type: "object" - {"name":"1705462312430827833502584767"}

I can manually fix them, but it would be nice if it worked with the same format it outputs.

@reacan
Copy link

reacan commented Feb 6, 2024

Wow guys, this issue is still here at 2024. So just like me you want to bulk delete all the keys from a KV namespace using Wrangler. Here is how to do it:

List all the KV namespaces associated with the account:

wrangler kv:namespace list

List all the keys from the given namespace and save them to a file:

wrangler kv:key list --namespace-id=string_of_numbers > raw_keys.json

Use this Bash script to convert the generated file into a format that can be fed to the bulk delete command:


#!/bin/bash

# Check if the correct number of arguments are provided
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <input_file> <output_file>"
    exit 1
fi

# Input and output filenames
input_file=$1
output_file=$2

# Extract names from input JSON and format them into a new JSON array
names=$(jq -r '.[].name' "$input_file" | sed 's/.*/"&"/' | tr '\n' ',' | sed 's/,$//' | sed 's/^/[/; s/$/]/')

# Output the formatted JSON array to the output file
echo "$names" > "$output_file"

chmod +x script.sh
./script.sh raw_keys.json keys_for_deletion.json

Finally delete all the keys from the namespace:

wrangler kv:bulk delete --namespace-id=string_of_numbers keys_for_deletion.json

Hope this comes handy to someone :)

@pdlug
Copy link

pdlug commented May 12, 2024

For anyone else looking for a simpler solution, you can transform the format using just jq:

wrangler kv:key list --namespace-id=${namespaceID} | jq '.[].name' | jq -s > keys_to_delete.json
wrangler kv:bulk delete --namespace-id=${namespaceID} keys_to_delete.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Change that will result in breaking existing behavior enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants