How can I remove all the gems installed using bundle install
in a particular RoR project. I don't want to uninstall gems that are used by other projects.

- 6,952
- 18
- 74
- 128
-
See: [What is the best way to uninstall gems from a rails3 project?](http://stackoverflow.com/questions/4667150/what-is-the-best-way-to-uninstall-gems-from-a-rails3-project) – vee Jan 27 '14 at 15:28
12 Answers
Since we're using ruby you could do something like this I guess:
bundle list | ruby -e 'ARGF.readlines[1..-1].each {|l| g = l.split(" "); puts "Removing #{g[1]}"; `gem uninstall --force #{g[1]} -v #{g[2].gsub(/\(|\)/, "")}`; }'
NOTE: Only lightly tested.

- 10,082
- 5
- 45
- 73
-
Tried this, and does not work. My hint is that the bundle list output is not a clean list of the specific gems: the gems in the bundle list do not include the version, there are bullet points and another header string that may be getting in the way. My two cents. – j4v1 Feb 27 '15 at 21:41
-
1
-
1for me on OSX worked with above except replacing `list` in first command with `show`. - wait - i'm not totally sure if it only uninstalled the gems from this particular Gemfile. It looks like it is: http://bundler.io/v1.11/bundle_show.html – MikeiLL Jun 27 '16 at 18:25
-
1Worked for me. Note, it also removes bundler so you need to `gem install bundler` then `bundle install`. – Ralph Aug 01 '16 at 21:19
-
1Worked out on a Fedora 31, but @martincito anwer using `rvm gemset empty` is far more efficient and clean. – psychoslave Apr 06 '20 at 06:27
There's no one simple way to remove all gems - let alone removing those within a specific bundle. You could try some of these suggestions: Uninstall all installed gems, in OSX?
Adapt to the bundle show
command instead of gem list
For the future, try this approach:
If you install your bundle locally like the example below, the gems won't be installed in your global gem directory. Then you can easily delete the installation folder to delete all gems of the bundle.
# install gems to project_root/vendor/bundle
bundle install --path vendor/bundle --without test
The path option is saved to .bundle/config just like all others and any subsequent bundle install
calls will use it unless you set it to something else or remove it from the config!

- 1
- 1

- 2,656
- 1
- 15
- 17
-
3+1 I always install them in vendor. It's easier to manage and keep your system clean. – andHapp Jan 27 '14 at 16:05
-
@KappaNossi : If I delete the folder. Would that free up the memory occupied by gems installed in the corresponding bundle? – nish Jan 27 '14 at 16:37
-
Yes of course. The downside is only that if multiple projects use the same gem, you will have multiple installations of it in the corresponding vendor folders instead of using one central gem in the global gem list. – KappaNossi Jan 27 '14 at 16:48
You can use (like Tobias says, if you are using RVM)
rvm gemset empty [gemset]
Directly on the gemset, for example
rvm gemset empty 2.0.0@server

- 389
- 4
- 10
It is actually as simple as
gem list --no-versions | xargs gem uninstall -a
If you are not using RVM/RBENV, you might hit into issue when gem attempts to uninstall system library which can fail. In that case, call uninstall command one by one to skip these.
gem list --no-versions | xargs -n1 gem uninstall -a

- 16,417
- 12
- 71
- 108
-
Love this solution. Easy, clean, faster than removing Ruby versions and reinstalling them. – Dave Kapp Jun 26 '20 at 14:14
If you're using rvm, you could of course just uninstall and reinstall the version of ruby under which you've installed the gems, i.e.
% rvm use
Using /home/ubuntu/.rvm/gems/ruby-2.2.1
% rvm uninstall 2.2.1
ruby-2.2.1 - #removing rubies/ruby-2.2.1..
ruby-2.2.1 - #removing default ruby interpreter.............
% rvm install 2.2.1
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/ubuntu/14.0/x86_64/ruby-2.2.1.tar.bz2
Checking requirements for ubuntu.
Requirements installation successful.
ruby-2.2.1 - #configure
ruby-2.2.1 - #download
ruby-2.2.1 - #validate archive
ruby-2.2.1 - #setup
ruby-2.2.1 - #gemset created /home/ubuntu/.rvm/gems/ruby-2.2.1@global
ruby-2.2.1 - #importing gemset /home/ubuntu/.rvm/gemsets/global.gems..............................
ruby-2.2.1 - #generating global wrappers........
ruby-2.2.1 - #gemset created /home/ubuntu/.rvm/gems/ruby-2.2.1
ruby-2.2.1 - #importing gemsetfile /home/ubuntu/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.2.1 - #generating default wrappers........
and now you have a ruby environment clean of any installed gems.

- 607
- 6
- 3
Another way from https://makandracards.com/jan0sch/9537-uninstall-all-ruby-gems-from-your-system (similar to rainkinz's answer and Ralph's comment). Here are some variations:
# if you're the root user:
gem list | cut -d" " -f1 | xargs -I % gem uninstall -aIx %
# if you're a non-root user:
gem list | cut -d" " -f1 | xargs -I % sudo gem uninstall -aIx %
# docker compose (if your service is named "web" running the root user):
docker-compose run web bash -c 'gem list | cut -d" " -f1 | xargs -I % gem uninstall -aIx %'
####
gem install bundler
# or if using docker compose:
docker-compose run web gem install bundler
# optionally reinstall gems:
bundle install
# or if using docker compose:
docker-compose run web bundle install
Breaking this down:
gem list
lists all gemscut -d" " -f1
takes the first columnxargs -I % gem uninstall -aIx %
callsgem uninstall -aIx
with each output line as an argument
Note that I specified the argument with -I
as %
and passed it directly for security:
xargs -I % gem uninstall -aIx %
instead of:
xargs gem uninstall -aIx
That's because xargs
has a security issue where options like -n
can be passed to its command and cause unexpected results. This can be demonstrated with the following example:
# correctly prints "-n hello" (with trailing newline):
echo '-n Hello' | xargs -I % echo % | xargs -I % echo %
# incorrectly prints "hello" (without trailing newline):
echo '-n Hello' | xargs echo

- 4,727
- 2
- 55
- 83
If your problem is similar to mine, then you want to uninstall all gems that were installed while testing GemFile
changes, in that case I simply used:
bundle clean
That uninstalled all the gems that are not specified in the GemFile
and GemFile.lock
.
I have not tested it but I suppose you could delete all lines from your Gemfile and run the above command to get rid of all the gems installed by the ROR project in the current directory.

- 1,083
- 2
- 12
- 22
I was using a docker
setup and here's how I've reverted gems installed by bundler
without rebuilding docker
.
Have a back up copy of Gemfile
and Gemfile.lock
.
Temporarily delete the contents of your Gemfile
(do not delete the file, just leave it blank) then (optionally) delete the Gemfile.lock
and run:
bundle clean --force
That should remove all gems installed by bundler
.
Then you can revert Gemfile
and Gemfile.lock
with your backup copy or git
.

- 2,415
- 1
- 13
- 26
Found solution for uninstalling all gems except of default:
Crete delete_gems.rb with
#!/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
Run this script:
sudo chmod 1777 delete_gems.rb
./delete_gems.rb
All gems will be removed except of default. Here link on original solution http://zanshin.net/2013/06/10/how-to-delete-all-ruby-gems/

- 321
- 4
- 11
As others have mentioned you could use rvm. To list all available gemsets
rvm gemset list
And then
rvm gemset empty <gemset-name>

- 301
- 2
- 9
How about:
rm -rf vendor/bundle/
Of if you're the timid type:
mv vendor/bundle vendor/bundle_old
Simple, easy, and guaranteed to flush out the cruft.

- 5,373
- 2
- 40
- 68