Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Homebrew uninstall script
#!/usr/bin/env ruby
#
# CLI tool for locating and removing a Homebrew installation
# http://brew.sh/
#
# Copyright (C) 2014 Stephen C. Benner
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author: Stephen Benner
# https://github.com/SteveBenner
#
# Contributors:
# - @AaronKulick
# - @lloeki
# - @lewismc
#
# NOTE: This script has ONLY been tested and verified for the following operating systems:
# - macOS 10.9
#
# TODO/ROADMAP
# - v1.1: test deletion code, make sure it can ALWAYS remove
# - v1.2: handle ENOENT errors (file not found)
# - v1.3: find and remove daemons installed by brew
# - v1.4: figure out why script can sometimes loop through multiple `locate_brew_path` runs
# - v1.5: fix git repo detection code so it doesn't break when brew is checked out but not installed
# - v1.6: improve script to work even without a Homebrew installation
#
require 'optparse'
require 'fileutils'
require 'open3'
$stdout.sync = true
# Default options
options = {
:quiet => false,
:verbose => true,
:dry_run => false,
:force => false,
:find_path => false
}
optparser = OptionParser.new do |opts|
opts.banner = 'Homebrew uninstaller' + $/
opts.on('-q', '--quiet', 'Quiet mode - suppress output.') do |setting|
options[:quiet] = setting
options[:verbose] = false
end
opts.on('-v', '--verbose', 'Verbose mode - print all operations.') { |setting| options[:verbose] = setting }
opts.on('-d', '--dry', 'Dry run - print results, but perform no actual operations.') do |setting|
options[:dry_run] = setting
end
opts.on('-f', '--force', 'Forces removal of files, bypassing prompt. USE WITH CAUTION!') do |setting|
options[:force] = setting
end
opts.on('-p', '--find-path', 'Output homebrew location if found, then exit.') do |setting|
options[:find_path] = setting
options[:quiet] = true
end
opts.on_tail('-h', '--help', '--usage', 'Display this message.') { puts opts; exit }
opts.on_tail('--version', 'Display script version.') { puts opts.version; exit }
end
optparser.version = '1.0'
optparser.summary_width = 16
optparser.parse!
$quiet = options[:quiet] # provides access to option value within methods
# Files installed into the Homebrew repository
BREW_LOCAL_FILES = %w[
.git
Cellar
Library/brew.rb
Library/Homebrew
Library/Aliases
Library/Formula
Library/Contributions
Library/LinkedKegs
]
# Files that Homebrew installs into other system locations
BREW_SYSTEM_FILES = %W[
#{ENV['HOME']}/Library/Caches/Homebrew
#{ENV['HOME']}/Library/Logs/Homebrew
/Library/Caches/Homebrew
]
$files = []
# This function runs given command in a sub-shell, expecting the output to be the
# path of a Homebrew installation. If given a block, it passes the shell output to
# the block for processing, using the return value of the block as the new path.
# Known Homebrew files are then scanned for and added to the file list. Then the
# directory is tested for a Homebrew installation, and the git index is added if
# a valid repo is found. The function won't run once a Homebrew installation is
# found, but it will accumulate untracked Homebrew files each invocation.
#
# @param [String] cmd A shell command to run
# @param [String] error_msg Message to print if command fails
#
def locate_brew_path(cmd, error_msg = 'check homebrew installation and PATH.')
return if $brew_location # stop testing if we find a valid Homebrew installation
puts "Searching for homewbrew installation using '#{cmd}'..." unless $quiet
# Run given shell command along with any code passed-in via block
path = `#{cmd}`.chomp
path = yield(path) if block_given? # pass command output to your own fancy code block
begin
Dir.chdir(path) do
# Search for known Homebrew files and folders, regardless of git presence
$files += BREW_LOCAL_FILES.select { |file| File.exist? file }.map {|file| File.expand_path file }
$files += Dir.glob('**/{man,bin}/**/brew*')
# Test for Homebrew git repository (use popen3 so we can suppress git error output)
repo_name = Open3.popen3('git remote -v') do |stdin, stdout, stderr|
stderr.close
stdout.read
end
if repo_name =~ /homebrew.git|Homebrew/
$brew_location = path
else
return
end
end
rescue StandardError # on normal errors, continue program
return
end
end
# Attempt to locate homebrew installation using a command and optional code block
# for processing the command results. Locating a valid path halts searching.
# TODO: Improve this code - refactor and add error handling
locate_brew_path 'brew --prefix'
locate_brew_path('which brew') { |output| File.expand_path('../..', output) }
locate_brew_path('command -v brew') { |output| File.expand_path('../..', output) }
locate_brew_path 'brew --prefix' do |output|
output = output.split($/).first
File.expand_path('../..', output)
end
# Found Homebrew installation
if $brew_location
if options[:find_path]
puts $brew_location
exit
end
unless options[:quiet]
puts "Homebrew found at: #{$brew_location}"
begin # record kegs and taps for later output
brewed = `brew list`
tapped = `brew tap`
rescue StandardError
end
end
# Collect files indexed by git
begin
Dir.chdir($brew_location) do
# Update file list (use popen3 so we can suppress git error output)
Open3.popen3('git checkout master') { |stdin, stdout, stderr| stderr.close }
$files += `git ls-files`.split.map {|file| File.expand_path file }
end
rescue StandardError => e
puts e # Report any errors, but continue the script and collect any last files
end
end
# Collect any files Homebrew may have installed throughout our system
$files += BREW_SYSTEM_FILES.select { |file| File.exist? file }
abort 'Failed to locate any homebrew files!' if $files.empty?
# DESTROY! DESTROY! DESTROY!
unless options[:force]
print "Delete #{$files.count} files? "
abort unless gets.rstrip =~ /y|yes/i
end
rm =
if options[:dry_run]
lambda { |entry| puts "deleting #{entry}" unless options[:quiet] }
else
lambda { |entry| FileUtils.rm_rf(entry, :verbose => options[:verbose]) }
end
puts 'Deleting files...' unless options[:quiet]
$files.each(&rm)
# Print a list of formulae and kegs that were removed as part of the uninstall process
if brewed
puts
puts 'The following previously installed formulae were removed:'
puts brewed
end
if tapped
puts
puts 'The following previously tapped kegs were removed:'
puts tapped
end
@gigaimage
Copy link

gigaimage commented Feb 24, 2015

I followed the instructions. Ran the script. It worked for me - Mac 10.10.2 Y

It ID 3493 files to be removed... I hope they were Homebrew install files only! :)

Thanks for a clear and one shot solution to overly debated/questioned prior solution

Update: Reinstall worked without any problem..

@giogadi
Copy link

giogadi commented Mar 18, 2015

Thanks for writing this.

When I ran this, however, it didn't actually remove any of the files installed by Homebrew. That is, it removed anything directly Homebrew related (Cellar, the config files) but left everything else that I installed using homebrew (bin, include, share, ...). Is this on purpose?

@jproberts14
Copy link

jproberts14 commented Mar 26, 2015

Thanks for creating this script Steve. The script worked great on my Mac Pro but when I tried to run it on my MacBook Pro, it's hanging right after it starts searching for the homebrew files. These are the 4 lines that I see before it hangs:

Searching for homebrew installation using 'brew --prefix'...
Searching for homebrew installation using 'which brew'...
Searching for homebrew installation using 'command -v brew'...
Searching for homebrew installation using 'brew --prefix'...

After a while, I got a message that said Delete 27 files? I said yes and it proceeded to delete those files. When I check out my /usr/local folder, some of the files are still there. When I compare the folder to the one on the Mac Pro, the files remaining are:

.gitignore
.yardopts
brew (in the bin folder)
codeofconduct.md
contributing.md
rubocop.yml (in Library folder - the Library also has the folders 3.2.6, 4.2, 4.3 which aren't on the Mac Pro)
License.txt
Readme.md
Homebrew folder under the Share folder (has 41 items in it) (the Homebrew folder is there on the MacPro but it's empty)
There is also a brew.1 file in the Man/Man1 folder as well
supporters.md

When I ran the script on the Mac Pro, it said it had found over 3,000 files.

Should I try to run the script again or is there another solution you can think of?

Thanks Steve!

Edit: I forgot to mention that I tried running it without sudo first, then ran it with sudo the second time.

@jproberts14
Copy link

jproberts14 commented Mar 28, 2015

Additional information regarding my uninstall issue. I remembered that the only difference in the installation between the mac pro and the macbook pro was xCode. On the mac pro, I had it installed via the command line interface. On the macbook pro, I went through the app store. Not sure if that has any relevance or not though.

@bsl787
Copy link

bsl787 commented Apr 1, 2015

Hello. I am very new. I had to re-install my Yosemite OS (iMac) because of a corrupted user profile. Homebrew insists on installing (trying) software to the corrupted user profile. I think I need to remove Homebrew. Looking at the directories in /usr/local/Cellar/, I notice some that appear to be ones I'd like to keep (python, octave, postgresql). If I run your script, do I lose these directories and re-install? How do I know which are the ones that Homebrew installed itself? For reference, permissions are root, group = wheel. Thank you.

BSL

@SteveBenner
Copy link
Author

SteveBenner commented Apr 8, 2015

@giogadi The directories you've mentioned, (bin/, include/, share/), among a few others, are created merely as a Unix convention (read more here) and they are used by many different programs. As it happens, Homebrew does not actually install anything in these folders—it is completely self-contained, relying on symbolic links from the original files to these locations, a very clean solution. When you use the uninstaller script, the symlinks should be removed along with Homebrew’s files. Anything remaining must have been installed by the user at some point, because the /usr/local/ directory itself is empty by default (and consequently safe to remove entirely!).

@jproberts14 I’m not sure how much help I can be to your situation, without more specific constraints to work with. Your Homebrew installation is definitely buggered—I would recommend deleting the entire contents of /usr/local/ and starting fresh. The fact your installation couldn’t be located using the first 3 commands indicates Homebrew wasn’t installed correctly in the first place, or that your shell environment is improperly configured. Any any rate, the ‘hanging’ is my last-ditch attempt to scrounge up Homebrew files in cases just like this… It’s far from spotless so again I recommend starting from scratch.

Bear in mind the script comes with several helpful options designed specifically with troubleshooting in mind. They are all inter-compatible. For instance, if you want to write a list of all the files potentially deleted by the script to a text file, you could run unbrew -d -f > results.txt which combines the ‘dry-run’ option (also known as no-op) with forced mode. _This is very cool._

As per convention, the script options and usage can be viewed by running it with -h or --help

@Roboji
Copy link

Roboji commented Aug 13, 2015

Hi Steve,
I experience the same problem as stated before by famousky.

$ sudo ./unbrew.rb
Searching for homewbrew installation using 'brew --prefix'...
./unbrew.rb:80:in ``': No such file or directory - brew (Errno::ENOENT)
from ./unbrew.rb:80:inlocate_brew_path' from ./unbrew.rb:106:in

'

I definitely have homebrew installed, so what am/can I do now??
Thanks!

@gethubd
Copy link

gethubd commented Aug 26, 2015

Hi Steve - thanks for the script. Just FYI I tried this on mac os x 10.6.8 and had the same problem as jproberts14. My guess is that the homebrew installs to different locations on 10.6 and the script doesn't see all of the files (however I am a complete novice so it's entirely just a guess). Your script didn't actually 'hang' as stated by jproberts14 - I was watching the activity in the activity monitor and it said ruby was very busy (with varying levels of cpu and disk activity) presumably looking for files. This took about 5 minutes before coming up with the "Delete 27 files ?" response. Most of the files it found were in the /local/ dir though it missed many others also in there.
I manually deleted all the remaining files in usr/local/ except for a few which were much older than the homebrew install.
Any tips on finding any other files to delete ( and I suspect there a a couple of thousand lurking around somewhere as between your script and manual deletion I think we only removed about 100-200), would be appreciated.
Thanks again for sharing.

@claudia1204
Copy link

claudia1204 commented Sep 27, 2015

Hi Steve,
I experienced the same problem.

sudo ./unbrew.rb

result:
Searching for homewbrew installation using 'brew --prefix'...
./unbrew.rb:105:in ``': No such file or directory - brew (Errno::ENOENT)
from ./unbrew.rb:105:inlocate_brew_path' from ./unbrew.rb:131:in

'

when I installed homebrew i quitted by control+C because of network problem. I do not know what should I do now....
Thanks!

@Roboji
use rm -rf /usr/local/Cellar /usr/local/.git I resolved problems, thanks~

@marktmiller
Copy link

marktmiller commented Oct 29, 2015

I tried Homebrew's uninstall command, which just messed up my installation. I couldn't use any of the packages afterward, nor could I reinstall them. They need to fix that! Your script cleaned out what needed to be cleaned, and made it so I could reinstall Homebrew from scratch. Thanks so much for posting this!

@lloeki
Copy link

lloeki commented Apr 11, 2016

Watch out, this script doesn't stop potentially running services beforehand, something to the effect of:

for d in {/Users/`whoami`,}/Library/Launch{Agents,Daemons}/homebrew.mxcl.*.plist; do launchctl unload $d; done

@flowoftime
Copy link

flowoftime commented Jul 18, 2016

Thank you very much. It works very well.

@lewismc
Copy link

lewismc commented Sep 20, 2016

@SteveBenner, if you have homebrew or homebrew-core already checked out to /usr/local then the script will fail, incorrectly thinking that you have Homebrew installed as oppose to just checked out locally into /usr/local.
After pruning both homebrew and homebrew-core from /usr/local and using your script it all went well thanks.

@cmfrtblynmb728
Copy link

cmfrtblynmb728 commented Sep 22, 2016

@SteveBenner I ran the script but it is stuck at the message "Searching for homewbrew installation using 'brew --prefix'..." for over an hour. Thanks!

Searching for homewbrew installation using 'brew --prefix'...
Searching for homewbrew installation using 'which brew'...
Searching for homewbrew installation using 'command -v brew'...
Searching for homewbrew installation using 'brew --prefix'...

This is all I see

@kevanchristmas
Copy link

kevanchristmas commented Nov 10, 2016

Thanks for the effort and continued comments/support - worked flawlessly for me

@techartist
Copy link

techartist commented Nov 26, 2016

@SteveBenner I am having the same issues as @cmfrtblynmb728. Any suggestions? No options were given on the command line I just ran ./unbrew.rb.

@anthonyrollett
Copy link

anthonyrollett commented Dec 11, 2016

Likewise, running on 10.10, needed to clear it out, seems to have worked exactly as intended. Much appreciated.

@anthonyrollett
Copy link

anthonyrollett commented Dec 11, 2016

I should have said that I found I had to clear brew out because I still use gfortran from HPC Mac OS X (http://hpc.sourceforge.net) and I needed to reinstall gcc and gfortran (versions 5.1). May not be the best approach these days, so happy to see further comments. Thanks again.

@binchuri
Copy link

binchuri commented Jun 29, 2019

Hello,
I was trying to use your script to uninstall Homebrew from Mac OSX Mojave (10.14.5) as it was unable to find the latest Xcode and Command Line Tools. However, it seems that the script is not actually deleting the files, they remain even after I run it. I am attaching the terminal output for your reference:

Apple$ ./unbrew.rb 
Searching for homewbrew installation using 'brew --prefix'...
Searching for homewbrew installation using 'which brew'...
Searching for homewbrew installation using 'command -v brew'...
Searching for homewbrew installation using 'brew --prefix'...
Delete 16 files? y
Deleting files...
rm -rf share/man/man1/brew-cask.1
rm -rf share/man/man1/brew.1
rm -rf bin/brew
rm -rf Homebrew/bin/brew
rm -rf share/man/man1/brew-cask.1
rm -rf share/man/man1/brew.1
rm -rf bin/brew
rm -rf Homebrew/bin/brew
rm -rf share/man/man1/brew-cask.1
rm -rf share/man/man1/brew.1
rm -rf bin/brew
rm -rf Homebrew/bin/brew
rm -rf usr/local/share/man/man1/brew-cask.1
rm -rf usr/local/share/man/man1/brew.1
rm -rf usr/local/bin/brew
rm -rf usr/local/Homebrew/bin/brew
Apples-MacBook-Air:unbrew Apple$ cd 
Apples-MacBook-Air:~ Apple$ cd /usr/l
lib/     libexec/ local/   
Apples-MacBook-Air:~ Apple$ cd /usr/l
lib/     libexec/ local/   
Apples-MacBook-Air:~ Apple$ cd /usr/local/
Apples-MacBook-Air:local Apple$ ls
Caskroom	etc		lib		sbin		var
Homebrew	gfortran	opt		share
bin		include		remotedesktop	texlive

Can you please advise what I should do? I also used unbrew.rb with sudo.

@ShadSterling
Copy link

ShadSterling commented Mar 10, 2022

I have vestigial homebrew files, but homebrew hasn't been installed for years, so I'm looking for an uninstaller that will clean up the files without having to reinstall homebrew. Unfortunately, like the official uninstaller, this doesn't work unless homebrew is installed.

@SteveBenner
Copy link
Author

SteveBenner commented Mar 18, 2022

2022 UPDATE:

I have added a list of fixes and improvements based on the last few years of feedback. Time to update the script! Thanks to the community for all the feedback and help testing this.

@lewismc, @lloeki these are great ideas and will be added as features

@SteveBenner
Copy link
Author

SteveBenner commented Mar 18, 2022

@Roboji, @claudia1204, @cmfrtblynmb728, @techartist, @binchuri

The output for all of your cases is similar, in that I can see a couple different possible bugs causing the same kind of issue for all of you.

There are several fixes I am going to implement, they are documented at the top of the script. It will take some time to test, but I’m confident I will soon know why it broke.

It would be helpful for those experiencing hangups using unbrew to kill it with CTL-C or similar, and report any errors that show up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment