babakfp
babakfp2mo ago

How to delete all data of a `Deno.Kv` DB file?

Hi 👋 Is there a way to delete all content inside a db file in openKv? Doing this doesn't work:
const keys = kv.list({ prefix: [] })
for await (const entry of keys) {
await kv.delete(entry.key)
}
const keys = kv.list({ prefix: [] })
for await (const entry of keys) {
await kv.delete(entry.key)
}
After running that and kv.list({ prefix: [] }) the length of it would be 0 but there are like 300 lines of stuff inside the DB file.
3 Replies
Deno AI Helper
Deno AI Helper2mo ago
Hello! 👋 I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you've provided, I can offer some general advice. The code you've written seems to be attempting to delete each key in the Deno.Kv database by iterating over the keys and calling kv.delete on each one. At first glance, the logic appears to be correct for deleting keys, but there are a couple of things to consider: 1. Ensure that kv is properly initialized and is referencing the correct Deno.Kv instance you intend to modify. 2. Verify that kv.list({ prefix: [] }) is the correct way to list all keys. Typically, a prefix is a string, so if you're trying to list all keys without a specific prefix, you might want to try using kv.list() without any arguments or with an empty string as the prefix. Here's a slightly modified version of your code that assumes you want to list all keys without a specific prefix:
const keys = kv.list(); // If you want to list all keys without a specific prefix
for await (const entry of keys) {
await kv.delete(entry.key);
}
const keys = kv.list(); // If you want to list all keys without a specific prefix
for await (const entry of keys) {
await kv.delete(entry.key);
}
Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of the advice given. If the issue persists, it might be helpful to look for more information in the Deno community or check if there are any updates in the documentation that address this use case.
babakfp
babakfp2mo ago
I can't even dislike!
This interaction failed
This interaction failed
Leokuma
Leokuma2mo ago
In Deploy I don't know. But if you're running KV locally, you could just delete and recreate the DB file I probably haven't answered your question though