0

Delete a VPC

by
Published Dec 20, 2024

To delete a VPC, send a DELETE request to `/v2/vpcs/$VPC_ID`. A 204 status code with no body will be returned in response to a successful request. The default VPC for a region can not be deleted. Additionally, a VPC can only be deleted if it does not contain any member resources. Attempting to delete a region's default VPC or a VPC that still has members will result in a 403 Forbidden error response.

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 VPC
7
 * To delete a VPC, send a DELETE request to `/v2/vpcs/$VPC_ID`. A 204 status
8
code with no body will be returned in response to a successful request.
9

10
The default VPC for a region can not be deleted. Additionally, a VPC can only
11
be deleted if it does not contain any member resources. Attempting to delete
12
a region's default VPC or a VPC that still has members will result in a
13
403 Forbidden error response.
14

15
 */
16
export async function main(auth: Digitalocean, vpc_id: string) {
17
  const url = new URL(`https://api.digitalocean.com/v2/vpcs/${vpc_id}`);
18

19
  const response = await fetch(url, {
20
    method: "DELETE",
21
    headers: {
22
      Authorization: "Bearer " + auth.token,
23
    },
24
    body: undefined,
25
  });
26
  if (!response.ok) {
27
    const text = await response.text();
28
    throw new Error(`${response.status} ${text}`);
29
  }
30
  return await response.json();
31
}
32