0

Delete a Block Storage Volume by Name

by
Published Dec 20, 2024

Block storage volumes may also be deleted by name by sending a DELETE request with the volume's **name** and the **region slug** for the region it is located in as query parameters to `/v2/volumes?name=$VOLUME_NAME&region=nyc1`. No response body will be sent back, but the response code will indicate success. Specifically, the response code will be a 204, which means that the action was successful with no returned body data.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Delete a Block Storage Volume by Name
7
 * Block storage volumes may also be deleted by name by sending a DELETE request with the volume's **name** and the **region slug** for the region it is located in as query parameters to `/v2/volumes?name=$VOLUME_NAME&region=nyc1`.
8
No response body will be sent back, but the response code will indicate success. Specifically, the response code will be a 204, which means that the action was successful with no returned body data.
9

10

11
 */
12
export async function main(
13
  auth: Digitalocean,
14
  name: string | undefined,
15
  region:
16
    | "ams1"
17
    | "ams2"
18
    | "ams3"
19
    | "blr1"
20
    | "fra1"
21
    | "lon1"
22
    | "nyc1"
23
    | "nyc2"
24
    | "nyc3"
25
    | "sfo1"
26
    | "sfo2"
27
    | "sfo3"
28
    | "sgp1"
29
    | "tor1"
30
    | "syd1"
31
    | undefined,
32
) {
33
  const url = new URL(`https://api.digitalocean.com/v2/volumes`);
34
  for (const [k, v] of [
35
    ["name", name],
36
    ["region", region],
37
  ]) {
38
    if (v !== undefined && v !== "" && k !== undefined) {
39
      url.searchParams.append(k, v);
40
    }
41
  }
42
  const response = await fetch(url, {
43
    method: "DELETE",
44
    headers: {
45
      Authorization: "Bearer " + auth.token,
46
    },
47
    body: undefined,
48
  });
49
  if (!response.ok) {
50
    const text = await response.text();
51
    throw new Error(`${response.status} ${text}`);
52
  }
53
  return await response.json();
54
}
55