uninstall all gems 31. May 2008

To perform batch operations in MacPorts you can use the predefined pseudo-portnames. RubyGems has no such thing, but instead it can handle regular expressions. So to uninstall all gems of your default RubyGems installation you can simply issue the following command.

sudo gem uninstall --a --ignore-dependencies .+

You may have to execute it twice before all gems are gone. If the command doesn’t work then try this shell script (thanks to melzz):

#!/bin/bash

GEMS=`gem list --no-versions`
for x in $GEMS ; do gem uninstall $x; done

Bharat Ruparel asked in the comments if there is a way to automatize the reinstallation of the gems. Yes, there is. This solution generates a file containing a list of all the gems and after removing all gems reads the gem names from this file to reinstall them. The solution does not however honor the installed version of the gems, so if you care about the versions you’re going to have to find a different way. I have tested this on Mac OS X, if you’re using Linux you’d want to replace the -E with an -r and escape the asterik with a backslash as pointed out in the comments.

gem list --no-versions | sed -E '/^(*|$)/d' > installed_gems
sudo gem uninstall --a --ignore-dependencies .+
cat installed_gems | xargs sudo gem install
rm installed_gems
 

Kommentare (9)

  1. Bharat Ruparel 33 days later

    Thank you. This saves me all kinds of time. Is there an automated way of installing gems so rebuilding is just as easy?

  2. Stefan 33 days later

    I have edited the posting. If you haven’t already wiped your gems, this might be what you’re looking for.

  3. stumble 288 days later

    Also, for Linux, you need to escape the * in the regexp:

    gem list --no-versions | sed -r '/^(\*|$)/d' > installed_gems

  4. Stefan 288 days later

    Thank you for your helpful comment, I have added a notice to the posting!

  5. acatl 480 days later

    hi, any idea why im getting this error:

    ERROR: While executing gem … (Gem::InstallError) cannot uninstall, check gem list -d .+

    I’m on OS X and ran the command: sudo gem uninstall –a –ignore-dependencies .+

  6. stefan 480 days later

    the problem might be, that you’re using an irregular dash. try entering the statement manually instead of copy & pasting it and see if it works! good luck!

  7. Ivan Storck 570 days later

    this is the version that worked for me: gem list --no-versions | xargs --interactive sudo gem uninstall -a -I

  8. boourns 731 days later

    Ivan, yours worked great, thanks. on ubuntu 10.04, trying to downgrade rails 3 beta to 2.3.8 by reinstalling everything

  9. Don Smith 806 days later

    http://gist.github.com/526232

    Thanks!

Kommentar schreiben

Markdown Syntax