Skip to content

Instantly share code, notes, and snippets.

@IanVaughan
Created June 9, 2012 20:37
Show Gist options
  • Save IanVaughan/2902499 to your computer and use it in GitHub Desktop.
Save IanVaughan/2902499 to your computer and use it in GitHub Desktop.
Uninstall all rbenv gems
#!/usr/bin/env bash
uninstall() {
list=`gem list --no-versions`
for gem in $list; do
gem uninstall $gem -aIx
done
gem list
gem install bundler
}
#rbenv versions --bare
RBENVPATH=`rbenv root`
echo $RBENVPATH
RUBIES=`ls $RBENVPATH/versions`
for ruby in $RUBIES; do
echo '---------------------------------------'
echo $ruby
rbenv local $ruby
uninstall
done
@PhilT
Copy link

PhilT commented Jul 31, 2013

Nice script!

Was looking for something like this. Gave me a surprise when I saw who wrote it!

Just used it. Works great!

@PhilT
Copy link

PhilT commented Jul 31, 2013

You might want to add "are you sure you want to remove everything?"!

@danielalvarenga
Copy link

Muito bom!

@exalted
Copy link

exalted commented Feb 20, 2014

This leaves .ruby-version in the current folder when it's done?

@reicheltd
Copy link

works! thx

@futhr
Copy link

futhr commented Oct 15, 2014

Perfect, thx!

@jez
Copy link

jez commented Dec 21, 2014

I wrote an updated version that addresses the .ruby-version issue and also allows you to specify a list of ruby versions whose gems should be installed as command line args:

https://gist.github.com/jez/cc2ba08062c6183a489c

@yourivdlans
Copy link

The above link doesn't seem to work :(

@jez
Copy link

jez commented Apr 19, 2015

Sorry about that, I had changed my GitHub username. It should be fixed now.

@dsapandora
Copy link

Works great for me. Thanks!!

@wellington1993
Copy link

Legal! Cool!

@ThinkTankShark
Copy link

Amazing script. Thanks!

@denislins
Copy link

+99

@antnruban
Copy link

Works great!! Thanks a lot!

@agrimm
Copy link

agrimm commented Feb 8, 2017

This is a great script. However, there's a minor hiccup: I got

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

Is there a fork that addresses this?

@lacostenycoder
Copy link

Nice one! I just wanted to remove one gem prefix for all ruby versions and was easy with this.
Just modified

list=`gem list --no-versions`

to

list=`gem list my_stupid_gem_prefix --no-versions`

Works great, thanks!

@b264
Copy link

b264 commented Jun 20, 2017

this== greatness

@florestankorp
Copy link

Wow, thanks for this script!

@konstantin0s
Copy link

I am new to Ubuntu and I want to know how do you run this script. Any suggestions?

@ymsrk
Copy link

ymsrk commented Jan 14, 2018

Works Thanks! Nice Script!

@marioabreu
Copy link

cool 👍

@charlesdebarros
Copy link

You are a life-saver. Thank you.
:)

@charlesdebarros
Copy link

charlesdebarros commented Oct 22, 2018

I am new to Ubuntu and I want to know how do you run this script. Any suggestions?
@konstantin0s
I am sure you may have found the solution by now but just in case, check this:
https://askubuntu.com/questions/38661/how-do-i-run-sh-files

@jcunanan05
Copy link

Thank you!

Copy link

ghost commented Feb 4, 2019

Thanks. Saved us the trouble

@mrvincenzo
Copy link

Thanks!

@coorasse
Copy link

I elaborated a version to cleanup only old versions of the gems:

#!/bin/sh
RBENVPATH=`rbenv root`
echo $RBENVPATH
RUBIES=`ls $RBENVPATH/versions`
for ruby in $RUBIES; do
  echo '---------------------------------------'
  echo $ruby
  rbenv local $ruby
  gem cleanup
done

@hdchinh
Copy link

hdchinh commented Jun 22, 2021

I elaborated a version to cleanup only old versions of the gems:

#!/bin/sh
RBENVPATH=`rbenv root`
echo $RBENVPATH
RUBIES=`ls $RBENVPATH/versions`
for ruby in $RUBIES; do
  echo '---------------------------------------'
  echo $ruby
  rbenv local $ruby
  gem cleanup
done

nice script, thank you

@Riveascore
Copy link

Riveascore commented May 4, 2024

Updated solution needed, because of the following header:

*** LOCAL GEMS ***

*** LOCAL GEMS ***

aasm (5.3.0)
abbrev (default: 0.1.1)
ace-rails-ap (4.3)
etc...

Fix:

--no-verbose is what removes the header.
(We may need another fix in the future if things change again)

#!/bin/bash

gem_remove() {
  gem list --no-versions --no-verbose | xargs gem uninstall -aIx
  gem list
}

gem_remove_all() {
  #rbenv versions --bare
  RBENVPATH=`rbenv root`
  echo $RBENVPATH
  RUBIES=`ls $RBENVPATH/versions`
  for ruby in $RUBIES; do
    echo '---------------------------------------'
    echo $ruby
    rbenv local $ruby
    gem_remove
  done
}

I don't include gem install bundler at the end.
Many times in Rails, Gemfile.lock will include a specific bundler version, so I prefer to just install that version manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment