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