Delete GitHub Actions caches for a repository (using a cache key)

Deletes one or more GitHub Actions caches for a repository, using a complete cache key. By default, all caches that match the provided key are deleted, but you can optionally provide a Git ref to restrict deletions to caches that match both the provided key and the Git ref. You must authenticate using an access token with the `repo` scope to use this endpoint. GitHub Apps must have the `actions:write` permission to use this endpoint.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 367 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Delete GitHub Actions caches for a repository (using a cache key)
6
 * Deletes one or more GitHub Actions caches for a repository, using a complete cache key. By default, all caches that match the provided key are deleted, but you can optionally provide a Git ref to restrict deletions to caches that match both the provided key and the Git ref.
7

8
You must authenticate using an access token with the `repo` scope to use this endpoint.
9

10
GitHub Apps must have the `actions:write` permission to use this endpoint.
11
 */
12
export async function main(
13
  auth: Github,
14
  owner: string,
15
  repo: string,
16
  key: string | undefined,
17
  ref: string | undefined
18
) {
19
  const url = new URL(
20
    `https://api.github.com/repos/${owner}/${repo}/actions/caches`
21
  );
22
  for (const [k, v] of [
23
    ["key", key],
24
    ["ref", ref],
25
  ]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "DELETE",
32
    headers: {
33
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43