Uninstall all installed gems, in OSX?

RubyRubygems

Ruby Problem Overview


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 development machine to use RVM. As they had been previously using the system-wide gem, he was experiencing many headaches when working with multiple projects. Essentially, he was the poster-child for an RVM convert.

How can I elegantly uninstall all of the gems on his OSX system?

Ruby Solutions


Solution 1 - Ruby

Rubygems >= 2.1.0

gem uninstall -aIx

a removes all versions
I ignores dependencies
x includes executables

Rubgems < 2.1.0

for i in `gem list --no-versions`; do gem uninstall -aIx $i; done

Solution 2 - Ruby

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.

Solution 3 - Ruby

A slighest different version, skipping the cut step, taking advantage of the '--no-version' option:

gem list --no-version |xargs gem uninstall -ax

Since you are removing everything, I don't see the need for the 'I' option. Whenever the gem is removed, it's fine.

Solution 4 - Ruby

First make sure you have at least gem version 2.1.0

gem update --system
gem --version
# 2.6.4

To uninstall simply run:

gem uninstall --all

You may need to use the sudo command:

sudo gem uninstall --all

Solution 5 - Ruby

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).

Solution 6 - Ruby

If you are using Rubygems version 2.1.0 or later, you can try: gem uninstall --all.

Solution 7 - Ruby

And for those of you who are here because you want to remove all gems with a certain prefix (ahem I'm looking at you aws-sdk!) you can run something like this:

gem list --no-version | grep "aws-sdk-" | xargs gem uninstall -aIx

Obviously put in your query instead of aws-sdk-. You need the -I in there to ignore dependencies.

Adopted form Ando's earlier answer

Solution 8 - Ruby

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

Solution 9 - Ruby

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!!

Solution 10 - Ruby

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

Solution 11 - Ruby

I did that not too long ago (same poster-child RVM switcher situation):

gem list | cut -d" " -f1 | sudo xargs gem uninstall -Iax

Takes the list of all gems (incl. version stuff), cuts it to keep only the gem name, then uninstalls all versions of such gems.

The sudo is only useful if you had gems installed system-wide, and should not be included unless necessary.

Solution 12 - Ruby

When trying to remove gems installed as root, xargs seems to halt when it encounters an error trying to uninstall a default gem:

sudo gem list | cut -d" " -f1 | xargs gem uninstall -aIx
# ERROR:  While executing gem ... (Gem::InstallError)
#    gem "test-unit" cannot be uninstalled because it is a default gem


This won't work for everyone, but here's what I used instead:

sudo for gem (`gem list | cut -d" " -f1`); do gem uninstall $gem -aIx; done

Solution 13 - Ruby

gem list --no-version | grep -v -e 'psych' -e 'rdoc' -e 'openssl' -e 'json' -e 'io-console' -e 'bigdecimal' | xargs sudo gem uninstall -ax

grep here is excluding default gems. All other gems will be uninstalled. You can also precede it with sudo in case you get permission issues.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncrftrView Question on Stackoverflow
Solution 1 - RubycrftrView Answer on Stackoverflow
Solution 2 - RubybswinnertonView Answer on Stackoverflow
Solution 3 - RubyThiago GanzarolliView Answer on Stackoverflow
Solution 4 - RubytypoView Answer on Stackoverflow
Solution 5 - RubyaercolinoView Answer on Stackoverflow
Solution 6 - RubyAlessandroView Answer on Stackoverflow
Solution 7 - RubyTrevor ElwellView Answer on Stackoverflow
Solution 8 - RubyHaris KrajinaView Answer on Stackoverflow
Solution 9 - RubyKirtikumar A.View Answer on Stackoverflow
Solution 10 - RubyAttenzioneView Answer on Stackoverflow
Solution 11 - RubyRomainView Answer on Stackoverflow
Solution 12 - RubyZazView Answer on Stackoverflow
Solution 13 - RubymoeabdolView Answer on Stackoverflow