0

Selectively Delete a Cluster and its Associated Resources

by
Published Dec 20, 2024

To delete a Kubernetes cluster along with a subset of its associated resources, send a DELETE request to `/v2/kubernetes/clusters/$K8S_CLUSTER_ID/destroy_with_associated_resources/selective`.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Selectively Delete a Cluster and its Associated Resources
7
 * To delete a Kubernetes cluster along with a subset of its associated resources,
8
send a DELETE request to `/v2/kubernetes/clusters/$K8S_CLUSTER_ID/destroy_with_associated_resources/selective`.
9
 */
10
export async function main(
11
  auth: Digitalocean,
12
  cluster_id: string,
13
  body: {
14
    load_balancers?: string[];
15
    volumes?: string[];
16
    volume_snapshots?: string[];
17
  },
18
) {
19
  const url = new URL(
20
    `https://api.digitalocean.com/v2/kubernetes/clusters/${cluster_id}/destroy_with_associated_resources/selective`,
21
  );
22

23
  const response = await fetch(url, {
24
    method: "DELETE",
25
    headers: {
26
      "Content-Type": "application/json",
27
      Authorization: "Bearer " + auth.token,
28
    },
29
    body: JSON.stringify(body),
30
  });
31
  if (!response.ok) {
32
    const text = await response.text();
33
    throw new Error(`${response.status} ${text}`);
34
  }
35
  return await response.json();
36
}
37