Skip to content

Instantly share code, notes, and snippets.

@scottjenson
Last active March 22, 2026 17:04
Show Gist options
  • Select an option

  • Save scottjenson/29bf412b1391b8b7da4dd134dcdf4540 to your computer and use it in GitHub Desktop.

Select an option

Save scottjenson/29bf412b1391b8b7da4dd134dcdf4540 to your computer and use it in GitHub Desktop.
shell script to remove old Mac wifi connections
#!/bin/bash
# Shell script to remove old wifi connections from your Mac
# save this as clean_wifi.sh
# do a chmod +x clean_wifi.sh in terminal
# run with a ./clean_wifi.sh
echo "Fetching saved Wi-Fi networks..."
# Ask for the Mac password once at the very beginning
sudo -v
# Fetch, clean, and pipe the list directly into the loop
networksetup -listpreferredwirelessnetworks en0 | tail -n +2 | sed 's/^[[:space:]]*//' | while IFS= read -r network; do
# Skip any blank lines
[ -z "$network" ] && continue
# Prompt the user for y/n/q, reading directly from the keyboard
read -p "Delete '$network'? (y/n/q): " choice </dev/tty
# Allow the user to quit the script early
if [[ "$choice" == [Qq]* ]]; then
echo "Aborting early. Your remaining networks were untouched."
break
fi
# Check if the user typed y or Y
if [[ "$choice" == [Yy]* ]]; then
sudo networksetup -removepreferredwirelessnetwork en0 "$network" >/dev/null 2>&1
echo " -> Removed!"
else
echo " -> Kept."
fi
done
echo "Cleanup complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment