47

It seems that ruby 2.0.0 has added "default" gems to the mix and makes them non removable by gem uninstall.

How can you remove all non default gems?

Nathan Lilienthal
  • 864
  • 1
  • 10
  • 16
  • Can you provide example of gems & how you installed Ruby 2.0 (manually? `rvm`?) – Marc-André Lafortune Feb 26 '13 at 22:54
  • 1
    I used rbenv to install ruby 2.0.0-p0. Gems that are "default" for me at least are: bigdecimal 1.2.0, io-console 0.4.2, json 1.7.7, minitest 4.3.2, psych 2.0.0, rake 0.9.6, rdoc 4.0.0, test-unit 2.0.0.0. `gem uninstall json` errors with gem "json" cannot be uninstalled because it is a default gem. – Nathan Lilienthal Feb 26 '13 at 23:20
  • 1
    Sorry, misunderstood. I never delete gems, not sure one would. Why do you need anything else than `gem cleanup`? – Marc-André Lafortune Feb 27 '13 at 03:11
  • 2
    Sometimes I just want to get rid of things. Cleanup is probably what most people need most of the time. There are times however I want to get rid of all gems to test bash scripts that need to install things like gems. – Nathan Lilienthal Feb 27 '13 at 03:41
  • `rvm` has gemsets, not sure about `rbenv` – Marc-André Lafortune Feb 27 '13 at 04:15
  • I'm working with a system atm that has ruby without `rvm` or `rbenv`. But I agree if I has `rvm` a simple `rvm gemset empty` would do :) – Nathan Lilienthal Feb 27 '13 at 04:44

4 Answers4

87

I used this one line script.

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

It ignores default gem errors and just proceeds. Simple and self-evident.

ihji
  • 1,456
  • 11
  • 19
  • That's a simpler solution for this problem, assuming rubygems continues to work this way :) – Nathan Lilienthal Feb 28 '13 at 16:30
  • Nice work! You don't see a lot of terse shell commands posted using a loop. – james_womack Apr 18 '13 at 17:39
  • 21
    Does not work. still get 'cannot be uninstalled because it is a default gem' errors. – Nippysaurus Aug 03 '13 at 04:23
  • 1
    @Nippysaurus It's normal. You can just ignore it. If you mean the command stops after the first error, type `set +e` and try again. – ihji Aug 20 '13 at 08:57
  • @ihji would know if this works `gem list --no-version | awk 'NR>1{ print $(NF-1) }' | xargs gem 2>/dev/null uninstall -aIx` it doesn't error but it also doesn't uninstall the gems. I assume the command still stops executing when error occurs (even if it gets redirected) – Integralist Dec 11 '13 at 12:04
  • If using zsh, you can be even shorter (only in the cli, not as a script), `for i in $(gem list --no-versions); gem uninstall -aIx $i`. – Pablo Olmos de Aguilera C. Feb 02 '19 at 23:44
13

First, go to the gems directory Like ../ruby/2.0.0-p195/lib/ruby/gems/2.0.0/specifications
You will find a directory named default, which including all the default gems shipped with ruby 2.0

Move all the *.gemspec stored in default dir to specifications dir and remove the empty default dir.

Then you can do whatever you want like old days.:-)

Jk1
  • 11,233
  • 9
  • 54
  • 64
Dolittle Wang
  • 686
  • 7
  • 7
6

I wrote a script in ruby to remove all non default gems.

https://gist.github.com/nixpulvis/5042764

This is needed now because unlike before 2.0.0 some gems are labeled "default" with the installation of ruby, and cannot be uninstalled with gem uninstall. This makes the previously popular methods for deleting all gems not work.

For reference here it is.
gem list | cut -d" " -f1 | xargs gem uninstall

Nathan Lilienthal
  • 864
  • 1
  • 10
  • 16
0

I have not yet found a better answer than to exclude the "default" gems:

/usr/local/bin/gem list --no-versions | \
grep -v -E "(bigdecimal|io-console|json|minitest|psych|rake|rdoc|test-unit)" | \
xargs --no-run-if-empty /usr/local/bin/gem uninstall --executables --user-install --all --force
Kevin
  • 6,665
  • 1
  • 17
  • 14