Skip to content

Instantly share code, notes, and snippets.

@navitronic
Last active August 4, 2026 00:37
Show Gist options
  • Select an option

  • Save navitronic/f4cd055d325038ac08c648e2f0dc80e8 to your computer and use it in GitHub Desktop.

Select an option

Save navitronic/f4cd055d325038ac08c648e2f0dc80e8 to your computer and use it in GitHub Desktop.
Forget bluetooth devices...
function forget_bluetooth_devices() {
local all=0
[[ "$1" == "--all" ]] && all=1
if ! command -v blueutil &>/dev/null; then
echo "blueutil is not installed. Install it with: brew install blueutil"
return 1
fi
local line address name type reply
local -a lines
lines=("${(@f)$(blueutil --paired)}")
local -A device_types
if command -v jq &>/dev/null; then
while IFS=$'\t' read -r address type; do
[[ -z "$address" ]] && continue
address=${address:l}
address=${address//:/-}
device_types[$address]="$type"
done < <(system_profiler -json SPBluetoothDataType 2>/dev/null | jq -r '
.SPBluetoothDataType[] |
((.device_connected // []) + (.device_not_connected // []))[] |
to_entries[] |
select(.value.device_minorType != null) |
"\(.value.device_address)\t\(.value.device_minorType)"
')
fi
for line in "${lines[@]}"; do
[[ -z "$line" ]] && continue
[[ "$line" != *"not connected"* ]] && continue
address=${line#address: }
address=${address%%,*}
name=${line#*name: \"}
name=${name%%\"*}
type=${device_types[$address]:-Unknown}
if (( all )); then
reply=y
else
read -k 1 "reply?Forget \"$name\" ($address, $type)? (y/n/q) "
echo
fi
case "$reply" in
q) return 0 ;;
y)
blueutil --unpair "$address" &>/dev/null
if blueutil --paired | grep -qi "^address: $address,"; then
echo "Failed to forget \"$name\""
else
echo "Forgot \"$name\""
fi
;;
esac
done
}

forget_bluetooth_devices

Interactively unpair Bluetooth devices via blueutil.

  • Lists only unconnected paired devices (connected devices are skipped).
  • Prompts per device: y unpair, n skip, q quit immediately.
  • Annotates each device with its type (Keyboard, Mouse, etc.) via system_profiler, when available.
  • Verifies the unpair actually took effect by re-checking blueutil --paired (blueutil's own exit code is unreliable — it reports success even when the device stays paired).
  • Requires blueutil (guides you to brew install blueutil if missing).

Usage

forget_bluetooth_devices        # interactive, prompts per device
forget_bluetooth_devices --all  # forgets every unconnected device, no prompts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment