Clear deno kv db

How can I clear out the database? using list requires the keys to be known, but I just want to clear out the instances that were added by mistake
3 Replies
Fifth-Normal-Form
// get a list of ALL keys in the KvDb
export async function getAllKeys() {
const allKeys = []
// the empty `prefix` below will return every key
const entries = db.list({ prefix: [] })
for await (const entry of entries) {
allKeys.push(entry.key)
}
return allKeys
}

// add known keys to this set
const knownSet = new Set()

// validate the key as known (wanted)
const known = (key) => knownSet.has(key);

/** delete all unknown rows from the db */
export async function clearUnknown() {
getAllKeys()
.then((keys) => {
keys.forEach( (key) => {
if(!known(key) db.delete(key);
})
})
}
// get a list of ALL keys in the KvDb
export async function getAllKeys() {
const allKeys = []
// the empty `prefix` below will return every key
const entries = db.list({ prefix: [] })
for await (const entry of entries) {
allKeys.push(entry.key)
}
return allKeys
}

// add known keys to this set
const knownSet = new Set()

// validate the key as known (wanted)
const known = (key) => knownSet.has(key);

/** delete all unknown rows from the db */
export async function clearUnknown() {
getAllKeys()
.then((keys) => {
keys.forEach( (key) => {
if(!known(key) db.delete(key);
})
})
}
You would first create an es6-Set of valid (known) keys.
You could then use the above clearUnknown method to filter your known keys to be kept.
This assumes you know the keys you want to keep.
You could retrieve the array of allKeys, and edit it to hold only known keys. Place that in knownSet
Official Jerome (fan account)
i didn't know about db.list({ prefix: [] }). that's super useful. thanks!
Fifth-Normal-Form
I use the above without the known test, to empty a kvdb. When this is used, its a good idea to close() the db to flush the write-ahead logs(WAL). another useful utility method Bulk-Transfer:
// utility to bulk transfer kv rows
export async function copyDB(from = 'data.db', to = 'copied.db') {

const fromDB = await Deno.openKv(from)
const destinationDB = (to.length > 0)
? await Deno.openKv(to)
: await Deno.openKv(); // if empty string was passed, use the default kvDb

const entries = fromDB.list({ prefix: [] })
for await (const entry of entries) {
await destinationDB.set(entry.key, entry.value);
}
fromDB.close()
destinationDB.close()
}
// utility to bulk transfer kv rows
export async function copyDB(from = 'data.db', to = 'copied.db') {

const fromDB = await Deno.openKv(from)
const destinationDB = (to.length > 0)
? await Deno.openKv(to)
: await Deno.openKv(); // if empty string was passed, use the default kvDb

const entries = fromDB.list({ prefix: [] })
for await (const entry of entries) {
await destinationDB.set(entry.key, entry.value);
}
fromDB.close()
destinationDB.close()
}

Did you find this page helpful?