0

Selectively Destroy a Droplet and its Associated Resources

by
Published Dec 20, 2024

To destroy a Droplet along with a sub-set of its associated resources, send a DELETE request to the `/v2/droplets/$DROPLET_ID/destroy_with_associated_resources/selective` endpoint.

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 Destroy a Droplet and its Associated Resources
7
 * To destroy a Droplet along with a sub-set of its associated resources, send a
8
DELETE request to the `/v2/droplets/$DROPLET_ID/destroy_with_associated_resources/selective`
9
endpoint.
10
 */
11
export async function main(
12
  auth: Digitalocean,
13
  droplet_id: string,
14
  body: {
15
    floating_ips?: string[];
16
    reserved_ips?: string[];
17
    snapshots?: string[];
18
    volumes?: string[];
19
    volume_snapshots?: string[];
20
  },
21
) {
22
  const url = new URL(
23
    `https://api.digitalocean.com/v2/droplets/${droplet_id}/destroy_with_associated_resources/selective`,
24
  );
25

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