Prune a val's versions.

Useful if you want to delete a bunch of versions.

All versions before keep_since that aren't in keep_versions will be deleted!!!

You can run it without passing commit to see a preview of what will happen.

Example

await prune_val("abcdefg", [3,6,8], 12, true);

Could output

Val: untitled_peachTakin, Current Version: 15
Deleting Versions: [ 1, 2, 4, 5, 7, 8, 9, 10, 11 ]
Deleting Version 1
Deleted
Deleting Version 2
Deleted
Deleting Version 4
Deleted
Deleting Version 5
Deleted
Deleting Version 6
Deleted
Deleting Version 7
Version already deleted, skipping
Deleting Version 8
Deleted
Deleting Version 9
Deleted
Deleting Version 10
Deleted
Deleting Version 11
Deleted
Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
export async function prune_val(
val_id: string,
keep_versions: number[],
keep_since: number,
commit = false,
): Promise<void> {
const resp = await fetch(`https://api.val.town/v1/vals/${val_id}`, {
headers: {
Authorization: "Bearer " + Deno.env.get("valtown"),
},
});
if (resp.status !== 200) {
console.error("Error fetching val:", resp);
return;
}
const val = await resp.json();
console.log(`Val: ${val.name}, Current Version: ${val.version}`);
const versions_to_delete = [];
for (let i = 1; i <= val.version; i++) {
if (i >= keep_since) {
break;
}
if (keep_versions.indexOf(i) == -1) {
versions_to_delete.push(i);
}
}
console.log("Deleting Versions:", versions_to_delete);
for (const v of versions_to_delete) {
console.log(`Deleting Version ${v}`);
const resp = await fetch(
`https://api.val.town/v1/vals/${val_id}/versions/${v}`,
{
headers: {
Authorization: "Bearer " + Deno.env.get("valtown"),
},
},
);
switch (resp.status) {
case 200: {
if (commit) {
const resp = await fetch(
`https://api.val.town/v1/vals/${val_id}/versions/${v}`,
{
method: "DELETE",
headers: {
Authorization: "Bearer " + Deno.env.get("valtown"),
},
},
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.
Comments
neverstew avatar

Readme output looks a bit off! It's deleting versions it should be keeping.

saolsen avatar

lol oops! Thanks, fixed it.