Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
Last active October 5, 2020 15:21
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save nixpulvis/5042764 to your computer and use it in GitHub Desktop.
Save nixpulvis/5042764 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

pvdb commented Sep 13, 2013

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

@Peeja
Copy link

Peeja 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

@kelhusseiny
Copy link

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

'

@jijiyangyu
Copy link

Works fine, nice job, thank you!

@stevemao
Copy link

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

@zchee
Copy link

zchee 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
Copy link

pvdb 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