Skip to content

Instantly share code, notes, and snippets.

@yunazuno
Created April 15, 2022 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yunazuno/eff90778833683212ac7c60dac78ec90 to your computer and use it in GitHub Desktop.
Save yunazuno/eff90778833683212ac7c60dac78ec90 to your computer and use it in GitHub Desktop.
Delete all devices on the Tailnet except for no expiry devices
#!/bin/bash
# Delete all devices on the Tailnet except for no expiry devices
# Released under the MIT license
set -eo pipefail
TAILSCALE_TAILNET=${TAILSCALE_TAILNET:-}
TAILSCALE_API_KEY=${TAILSCALE_API_KEY:-}
SCRIPT_REAL_RUN=${SCRIPT_REAL_RUN:false}
if [ -z "${TAILSCALE_API_KEY}" ]; then
echo "[ERROR] TAILSCALE_API_KEY is not set"
exit 1
fi
if [ -z "${TAILSCALE_TAILNET}" ]; then
echo "[ERROR] TAILSCALE_TAILNET is not set"
exit 1
fi
if [ "${SCRIPT_REAL_RUN}" != "true" ]; then
echo "[INFO] DRY RUN"
fi
devices=$(curl -s -u "${TAILSCALE_API_KEY}:" "https://api.tailscale.com/api/v2/tailnet/${TAILSCALE_TAILNET}/devices" | jq -r '.devices[] | select(.keyExpiryDisabled == false) | .id')
for device_id in $devices; do
echo "[INFO] Deleting" $device_id
if [ "${SCRIPT_REAL_RUN}" = "true" ]; then
curl -X DELETE -s -u "${TAILSCALE_API_KEY}:" "https://api.tailscale.com/api/v2/device/${device_id}"
sleep 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment