There are instances where I would like to revert and uninstall all previous gem installations.
For instance, I needed to assist a friend migrate their rails developm
If you are using Rubygems version 2.1.0 or later, you can try: gem uninstall --all
.
The only command helped me to cleanup all gems and ignores default gems, which can't be uninstalled
for x in `gem list --no-versions`; do gem uninstall $x -a -x -I; done
Use either
$ gem list --no-version | xargs gem uninstall -ax
or
$ sudo gem list --no-version | xargs sudo gem uninstall -ax
Depending on what you want, you may need to execute both, because "gem list" and "sudo gem list" provide independent lists.
Do not mix a normal "gem list" with a sudo-ed "gem uninstall" nor the other way around otherwise you may end up uninstalling sudo-installed gems (former) or getting a lot of errors (latter).
You could also build out a new Gemfile and run bundle clean --force
. This will remove all other gems that aren't included in the new Gemfile.
If you like doing it using ruby:
ruby -e "`gem list`.split(/$/).each { |line| puts `gem uninstall -Iax #{line.split(' ')[0]}` unless line.strip.empty? }"
Cheers
Rubygems >= 2.1.0
gem uninstall -aIx
If Terminal returns below error
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
Then write above command as below
sudo gem uninstall -aIx
And enter your mac os account password Done!!