-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete-layers.mjs
62 lines (54 loc) · 1.48 KB
/
delete-layers.mjs
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
51
52
53
54
55
56
57
58
59
60
61
62
import {
LambdaClient,
ListLayersCommand,
ListLayerVersionsCommand,
DeleteLayerVersionCommand,
} from "@aws-sdk/client-lambda";
const regions = [
"us-east-1",
"us-west-2",
"eu-west-1",
"ap-northeast-1",
"ap-southeast-1",
"ap-southeast-2",
"eu-central-1",
"sa-east-1",
];
// your layer name
const LayerName = "napi-rs-canvas";
// NOTE: This will only delete last 50 layers, keep running to delete all.
async function deleteOldLayerVersions(region) {
const lambdaClient = new LambdaClient({ region });
const layerVersionsResponse = await lambdaClient.send(
new ListLayerVersionsCommand({ LayerName })
);
const layerVersions = layerVersionsResponse.LayerVersions;
if (!layerVersions?.length) {
console.log("No layer versions found");
return;
}
for (let i = 0; i < layerVersions.length - 1; i++) {
const version = layerVersions[i].Version;
lambdaClient
.send(
new DeleteLayerVersionCommand({
LayerName: LayerName,
VersionNumber: version,
})
)
.then(() =>
console.log(`Deleted ${LayerName} version ${version} in ${region}`)
);
}
}
for (const region of regions) {
try {
console.log(`Processing region ${region}`);
// don't make this async, you'll be rate limited
await deleteOldLayerVersions(region);
console.log(`Finished processing region ${region}`);
} catch (error) {
console.error(`Error processing region ${region}:`, error);
}
}
console.log("Done.");