This is a feature request.
e.g. nvm uninstall 6 should remove all 6.x.x versions
I think it would be very unintuitive if the version matching logic worked any differently for uninstall than for anything else.
I suppose i could see an --all option or something, that would uninstall all matching versions, but the risk of uninstalling something unintended is high - and you can write your own shell command to do this already.
You could also prompt user a yes/no question
Versions to be removed: <list versions here...>
Proceed? [Y/n]
nvm’s commands all work in a noninteractive terminal, so prompting isn’t a feature we have available.
Then --all would be enough.
As for the risk of unintended removal, user has already insisted in removing all matching versions by adding --all option (making an extra effort).
Maybe nvm uninstall 6.* as an option? Personally I wouldn't mind being able to further navigate a "version-expression" with e.g. 8.11.*.
@fl0w Shell may interpret 6.* and 8.11.* as _glob pattern_. For example: When you have some files named 6.txt, 8.11.txt, shell might end up execute nvm uninstall --all 6.txt/nvm uninstall --all 8.11.txt instead.
Yes, asterisks will likely never be part of nvm because of the inherent risk with shell globs.
A workaround is to simply delete the nvm directories for all these versions. For example, to delete all v6* versions:
find ~/.nvm/versions/node -mindepth 1 -maxdepth 1 -name 'v6*' -exec rm -fr {} \;
This may leave parts of your nvm configuration inconsistent though. I tried it and noticed no real problems, but maybe @ljharb can comment on whether this approach is reasonable.
@jcsahnwaldt please don’t do that; a better approach would be to use nvm ls 6 and run nvm uninstall for each of them.
I'd prefer that myself, but I failed to write a script that works correctly. With which awk / sed / grep / etc. commands would you parse the output of nvm ls --no-colors 6?
I'd probably start with nvm ls --no-colors | egrep -o 'v\d+.\d+.\d+'? nvm ls --no-colors | egrep -o 'v6.\d+.\d+' | uniq seems to filter it down to v6 versions pretty well.
Prints v6.16.0 on my machine, although I don't have any v6 version installed. It picks up the version of the lts/boron alias.
In some cases, it may be useful to also get versions that are not installed, but I guess in most cases, the user will only want the installed versions. We would probably have to have another grep in the pipeline.
(In the case of nvm uninstall, it doesn't matter much: nvm uninstall v6.16.0 on my machine prints an error message, but return code is 0.)
Ah, that's true.
You could use nvm_ls (the internal function) but it's ideal not to have to rely on that. I suppose nvm ls --no-alias would also be useful.
Yes, nvm_ls is exactly what I'm looking for! :-) But I understand that one shouldn't rely on it.
Yeah, I think something like --no-alias would definitely be useful. See #2005.
One little thing: When I use a pattern that matches no installed version, nvm_ls prints N/A (to stdout) and returns 3. I guess for some scripts it might be better if nvm_ls simply did nothing in this case. Like ls on an empty directory. On the other hand, ls foo also signals an error when foo doesn't exist, so I guess nvm_ls should print N/A to stderr and return an error code.
nvm_ls is an internal function, so I don't think it's that important what conventions it follows; I'm more interested in that argument for the external API.
I'd probably start with
nvm ls --no-colors | egrep -o 'v\d+.\d+.\d+'?nvm ls --no-colors | egrep -o 'v6.\d+.\d+' | uniqseems to filter it down to v6 versions pretty well.
I want to uninstall stale installed node versions from my machine so I try to list first with (note that stale version lines do not contain "->" as I see the pattern):
❯ nvm ls --no-colors | grep -o '^[[:blank:]]*v[0-9]*.[0-9]*.[0-9]*' | tr -d '[[:blank:]]v'
13.2.0
13.6.0
But if I chain with xargs later, don't know why but it results in weird error:
❯ nvm ls --no-colors | grep -o '^[[:blank:]]*v[0-9]*.[0-9]*.[0-9]*' | tr -d '[[:blank:]]v' | xargs -I % nvm uninstall %
xargs: nvm: No such file or directory
Hope that nvm will have a way to support uninstalling stale versions. Highly voted for this issue to be resolved soon...
Most helpful comment
A workaround is to simply delete the nvm directories for all these versions. For example, to delete all
v6*versions:find ~/.nvm/versions/node -mindepth 1 -maxdepth 1 -name 'v6*' -exec rm -fr {} \;This may leave parts of your nvm configuration inconsistent though. I tried it and noticed no real problems, but maybe @ljharb can comment on whether this approach is reasonable.