Using jq to help remove headscale nodes by name

Headscale CLI requires that you remove a node by id rather than name. jq to the rescue!

You can get the id of a node by name with:

headscale nodes list --output json | jq '.[] | select(.given_name == "'"$NODE_NAME"'") | .id'

So you can script the removal with:

#!/bin/bash -eu
#
#  Remove a headscale node

NODE_NAME="$1"

NODE_ID=$(headscale nodes list --output json | jq '.[] | select(.given_name == "'"$NODE_NAME"'") | .id')
if [ -z "$NODE_ID" ]; then
    echo "No node found"
    exit 0
fi

headscale node delete --force --identifier "$NODE_ID"