summaryrefslogtreecommitdiff
path: root/functions/destroy_vps_by_label.sh
blob: 09d807e12c2f8ee68e972ef6d8774e9d81e1204c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
destroy_vps_by_label() {
  LABEL="$1"
  echo "Looking for VPS with label '$LABEL'..."
  LINODE_ID=$(curl -s -H "Authorization: Bearer $LINODE_API_TOKEN" \
    https://api.linode.com/v4/linode/instances | \
    jq -r --arg LABEL "$LABEL" '.data[] | select(.label == $LABEL) | .id')

  if [ -z "$LINODE_ID" ]; then
    echo "Error: No Linode found with label '$LABEL'"
    exit 1
  fi

  read -rp "Are you sure you want to destroy VPS '$LABEL' (ID: $LINODE_ID)? [y/N] " confirm
  if [[ "$confirm" =~ ^[Yy]$ ]]; then
    echo "Destroying Linode with ID $LINODE_ID (label: $LABEL)..."
    HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE \
      https://api.linode.com/v4/linode/instances/$LINODE_ID \
      -H "Authorization: Bearer $LINODE_API_TOKEN")

    if [[ "$HTTP_STATUS" == "204" ]]; then
      echo "✅ Linode $LABEL (ID $LINODE_ID) has been destroyed."
    else
      echo "❌ Failed to destroy VPS. HTTP status: $HTTP_STATUS"
    fi
  else
    echo "Cancelled. VPS '$LABEL' not destroyed."
  fi
}