Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove all non default gems. For ruby 2.0.0
#!/usr/bin/env ruby
# Remove all gems EXCEPT defaults :)
`gem list -d`.split(/\n\n^(?=\w)/).each do |data|
match = data.match(/(?<name>([^\s]+)) \((?<versions>.*)\)/)
name = match[:name]
versions = match[:versions].split(', ')
if match = data.match(/^.*\(([\d\.]*),? ?default\): .*$/)
next if match[1].empty? # it's the only version if this match is empty
versions.delete(match[1] || versions[0])
end
versions.each { |v| system "gem uninstall -Ix #{name} -v #{v}" }
end
@nixpulvis

This comment has been minimized.

Copy link
Owner Author

commented Feb 27, 2013

Run without saving by copying one of these into your shell.

CURL curl -Ls raw.github.com/gist/5042764 | ruby
WGET wget -qO- raw.github.com/gist/5042764 | ruby

@pvdb

This comment has been minimized.

Copy link

commented Sep 13, 2013

👍 ... works straight out of the box; thanks for sharing!

@Peeja

This comment has been minimized.

Copy link

commented Jan 16, 2014

GitHub appears to have changed the way raw URLs work.

Updated: Run without saving by copying one of these into your shell.

CURL: curl -Ls https://gist.github.com/nixpulvis/5042764/raw/7daa611e707f1884eb701551464bfd1dc9e7a953/gem-reset | ruby
WGET: wget -qO- https://gist.github.com/nixpulvis/5042764/raw/7daa611e707f1884eb701551464bfd1dc9e7a953/gem-reset | ruby

@Azzurrio

This comment has been minimized.

Copy link

commented Feb 15, 2014

-:4:in split': invalid byte sequence in US-ASCII (ArgumentError) from -:4:in

'

@jijiyangyu

This comment has been minimized.

Copy link

commented Jun 22, 2014

Works fine, nice job, thank you!

@stevemao

This comment has been minimized.

Copy link

commented Oct 22, 2014

ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

@zchee

This comment has been minimized.

Copy link

commented Apr 19, 2015

I'm fork and fixed.
Exclude my favorite gems during gem uninstall.

If you have a gem that want to keep, it is preferable to exclude them by fork.
https://gist.github.com/zchee/d2f100f1322fd6322a44

curl -Ls https://gist.githubusercontent.com/zchee/d2f100f1322fd6322a44/raw/2b129e3136baf839d9fb530fd215800a733c1dc8/gem-reset | ruby
@pvdb

This comment has been minimized.

Copy link

commented Jun 4, 2018

@nixpulvis - any ideas/suggestions for not only skipping the so-called default gems, but also the so-called bundled gems? see https://stdgems.org/ for their respective definitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.