How to uninstall all Ruby gems

Sometimes you want to remove all installed gems from your computer. This was the case for me when I upgraded my projects from Ruby 2.5.1 to 2.5.3 in order to clean things up in 2.5.1 and regain disk space.

Lots of Google entries for such a search point to:

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

But you end up with this kind of error:

ERROR:  While executing gem ... (Gem::InstallError)
    gem "bigdecimal" cannot be uninstalled because it is a default gem

Playing Whac-A-Mole filtering default gems:

gem list --no-version | grep -vE "^(bigdecimal|bundler|cmath|csv|date|dbm|etc|fcntl|fiddle|fileutils|gdbm|io-console|ipaddr|openssl|psych|rdoc|scanf|sdbm|stringio|strscan|webrick|zlib)$" | xargs gem uninstall -aIx

The final command

Since Rubygems 2.1, without gem names, it just removes all of them! So it’s simply:

gem uninstall -aIx

Details of options:

  • -a Uninstall all matching versions
  • -I Ignore dependency requirements while uninstalling
  • -x Uninstall applicable executables without confirmation

And voilà.

Previous article: Adding a staging environment to Rails

Maxime Garcia
Maxime Garcia
CTO @ Electra
Living in Paris