Skip to content

Instantly share code, notes, and snippets.

@jeremywadsack
Created August 28, 2017 19:59
Show Gist options
  • Save jeremywadsack/489d8868797b836fedbe74c89a634957 to your computer and use it in GitHub Desktop.
Save jeremywadsack/489d8868797b836fedbe74c89a634957 to your computer and use it in GitHub Desktop.
Remove unused rvm gemsets
# Safely cleans up your rvm Gemsets with the following:
# - Deletes gemsets associated with older rubies than the latest (if more than one)
# - Runs `bundle clean` to remove unused gems in latest gemsets
# - Removes rubies (and related gemsets) that are no longer used by a gemset
gemsets = Dir["#{ENV['HOME']}/.rvm/gems/*"].select do |folder|
folder.include?("@")
end
gemsets = gemsets.map do |folder|
File.basename(folder)
end
gemsets = gemsets.group_by do |folder|
folder.split("@").last
end
rubies = []
gemsets.each do |name, gemsets|
next if name == "global"
sets = gemsets.sort
keep = sets.pop
# Remove any gemsets that are bound to older versions of ruby than the latest
sets.each do |gemset|
(ruby, set) = gemset.split("@")
`rvm use #{ruby} do rvm --force gemset delete #{set}`
end
# Clean up unused gems in the gemset we're keeping
`rvm #{keep} do bundle clean --force`
rubies << keep.split("@").first
end
# Remove rubies (and gemsets) that we no longer need
gemsets["global"].each do |gemset|
(ruby, set) = gemset.split("@")
next if rubies.include?(ruby)
`rvm remove #{ruby}`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment