Bader Nasser's Blog

How to uninstall Node.js versions downloaded using nvm

Published on: Last updated on:

To uninstall unwanted node versions downloaded using nvm:

  • Open the terminal and run:

W=(16 17);WD=();V=$(nvm ls --no-colors --no-alias | cut -d'v' -f 2 | cut -d' ' -f 1 | tr '\n' ' ');for w in "${W[@]}";do WD+=("$(nvm version "$w" | cut -d'v' -f 2)");done;for v in ${V[@]};do if [[ " ${WD[*]} " =~ $v ]];then echo "Skipping v$v";else echo "Uninstalling v$v";nvm uninstall "$v";fi;done

  • Or:

    1. Add the following function to ~/.bash_profile or ~/.bashrc

      function uninstallNodeVersions {
      	local wantedVersions=(16 17)
      	# credit: https://stackoverflow.com/a/1951523
      	local wantedDownloadedVersions=()
      	local allVersions
      	allVersions=$(nvm ls --no-colors --no-alias | cut -d'v' -f 2 | cut -d' ' -f 1 | tr '\n' ' ')
      	for wantedVersion in "${wantedVersions[@]}"; do
      		wantedDownloadedVersions+=("$(nvm version "${wantedVersion}" | cut -d'v' -f 2)")
      	done
      	echo "Wanted node versions:" "${wantedVersions[@]}"
      	echo "Downloaded node versions:" "${wantedDownloadedVersions[@]}"
      	echo "All node versions:" "${allVersions[@]}"
      	# shellcheck disable=SC2068
      	for version in ${allVersions[@]}; do
      		# credit: https://stackoverflow.com/a/15394738
      		if [[ " ${wantedDownloadedVersions[*]} " =~ ${version} ]]; then
      			echo "Skip uninstalling node ${version} ..."
      		else
      			echo "Uninstalling node v${version} ..."
      			nvm uninstall "${version}"
      		fi
      	done
      }

    2. Open the terminal and run the function: uninstallNodeVersions

    3. Enjoy!

All rights reserved © 2022