29 lines
1021 B
Bash
Executable File

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
}