88

After updating a bundle, you will have some gems that may be obsolete -- since a newer version of that gem has been installed. There is no such command under the bundle executable i.e. bundle clean. How does one get rid of these obsolete gems?

This is an attempt to reduce slug size in my rails app.

Igbanam
  • 5,904
  • 5
  • 44
  • 68

8 Answers8

134

If you are using Bundler 1.1 or later you can use bundle clean, just as you imagined you could. This is redundant if you're using bundle install --path (Bundler manages the location you specified with --path, so takes responsibility for removing outdated gems), but if you've used Bundler to install the gems as system gems then bundle clean --force will delete any system gems not required by your Gemfile. Blindingly obvious caveat: don't do this if you have other apps that rely on system gems that aren't in your Gemfile!

Pat Shaughnessy has a good description of bundle clean and other new additions in bundler 1.1.

David Waller
  • 3,057
  • 4
  • 27
  • 28
  • 30
    For those using rvm, note that `bundle clean --force` doesn't touch your global gems, but only those in your current gemset. So `bundle clean --force` produces the same result as and is faster then `rvm gemset empty` followed by `bundle install`. – Gabe Kopley Jul 18 '13 at 18:21
  • 19
    DISCLAIMER: "Bundle clean --force" can blow away a large number of gems if you're not careful. That should be the first line of this response... especially since it has so many votes. BE CAREFUL! – jerrylroberts Aug 09 '13 at 20:26
  • @jerrylroberts Why is it a big deal that some gems get removed? If everything is in your gemfile, why should anyone care? How would a new person build the project from scratch? – Thomas Apr 25 '23 at 14:06
39

If you're using RVM you may use rvm gemset empty for the current gemset - this command will remove all gems installed to the current gemset (gemset itself will stay in place). Then run bundle install in order to install actual versions of gems. Also be sure that you do not delete such general gems as rake, bundler and so on during rvm gemset empty (if it is the case then install them manually via gem install prior to bundle install).

trushkevich
  • 2,657
  • 1
  • 28
  • 37
24

If you are using RVM you can install your gems into gemsets. That way when you want to perform a full cleanup you can simply remove the gemset, which in turn removes all the gems installed in it. Your other option is to simply uninstall your unused gems and re-run your bundle install command.

Since bundler is meant to be a project-per-project gem versioning tool it does not provide a bundle clean command. Doing so would mean the possibility of removing gems associated with other projects as well, which would not be desirable. That means that bundler is probably the wrong tool to use to manage your gem directory. My personal recommendation would be to use RVM gemsets to sandbox your gems in certain projects or ruby versions.

Pan Thomakos
  • 34,082
  • 9
  • 88
  • 85
13

Honestly, I had problems with bundler circular dependencies and the best way to go is rm -rf .bundle. Save yourselves the headache and just use the hammer.

Tyler Brock
  • 29,626
  • 15
  • 79
  • 79
  • 1
    I had to do this to get it to stop bugging me about "you must call bundle install with --no-deployment". Thanks – Kevin Aug 27 '13 at 03:53
  • 2
    Thanks for the idea. Although for me, using a bigger hammer helped. `rvm implode --force` and then reinstall rvm – Ryan Aug 21 '14 at 18:16
1

When searching for an answer to the very same question I came across gem_unused.
You also might wanna read this article: http://chill.manilla.com/2012/12/31/clean-up-your-dirty-gemsets/
The source code is available on GitHub: https://github.com/apolzon/gem_unused

JJD
  • 50,076
  • 60
  • 203
  • 339
1

I assume you install gems into vendor/bundle? If so, why not just delete all the gems and do a clean bundle install?

Leonid Shevtsov
  • 14,024
  • 9
  • 51
  • 82
  • my vendor directory only keeps track of assets and installed plugins. I'm on Rails 3.1 – Igbanam Oct 27 '11 at 06:20
  • Then what's the reason to remove unused gems if they aren't copied on deploy? They don't take up that much space. – Leonid Shevtsov Oct 27 '11 at 08:08
  • i am deploying this rails app on an intranet; meaning I handle the files and the environment locally. if you know of any solutions I can use for this, please share. – Igbanam Oct 27 '11 at 08:52
0

Just remove the obsolete gems from your Gemfile. If you're talking about Heroku (you didn't mention that) then the slug is compiled each new release, just using the current contents of that file.

Gareth
  • 133,157
  • 36
  • 148
  • 157
-2

Just execute, to clean gems obsolete and remove print warningns after bundle.

bundle clean --force
Marcello B.
  • 4,177
  • 11
  • 45
  • 65
codevb
  • 83
  • 1
  • 7