Skip to content

Instantly share code, notes, and snippets.

@katychuang
Last active January 30, 2024 12:20
Show Gist options
  • Save katychuang/10439243 to your computer and use it in GitHub Desktop.
Save katychuang/10439243 to your computer and use it in GitHub Desktop.
remove mongodb that was installed via brew
#!/usr/bin/env sh
# first check to see if mongo service is running. you can't delete any files until the service stops so perform a quick check.
launchctl list | grep mongo
# NOTE: the pipe | symbol means the commands on the right performs on the output from the left
# grep is a string search utility. `grep mongo` means search for the substring mongo
# use the unload command to end the mongo service. this is required to 'unlock' before removing the service.
# first look for the file to delete
MONGO_SERVICE_FILE=$(ls ~/Library/LaunchAgents/*mongodb*)
# the following produces something like launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
launchctl unload $MONGO_SERVICE_FILE
# NOTE: if there is an error with the unload command it means it's not recognizing the correct filename
# you can try searching for the correct filename within the ~/Library/LaunchAgents/ folder. see line 39 for the command.
# NOTE: another mention about the note above to ensure reader sees the message above about correct filename.
# then you can remove the service. the name of the service after the word remove should match the output from running the command shown in line 4
# basically you're filling in the pattern launchctl remove <service-name>
launchctl remove homebrew.mxcl.mongodb
# kill the process
pkill -f mongod
# remove the mongodb service file. again, please make sure the filename is correct.
# this command removes the plist file that was found above. It should create a command that works to the effect of
# rm -f ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
rm -f $MONGO_SERVICE_FILE
# remove data directories
rm -rf /usr/local/var/mongod
# NOTE: the flag options here include both r and f
# using -rf together means you're telling the machine to 'delete all items in the specified path subtree without confirmation'
# uninstal mongodb using brew
brew uninstall mongodb
# double check existence of mongodb in both folders
ls -al /usr/local/bin/*mongo*
ls -al ~/Library/LaunchAgents/*mongo*
# NOTE: The asterisk indicate wildcard using regular expression (regex) notation along with substring `mongo`
# This regex syntax has same purpose as using grep when searching for the existence of filename(s)
# so instead of soley using the `ls` command you can also combine with grep `ls -al ~/Library/LaunchAgents | grep mongo`
# now you should have it entirely removed and can reinstall mongodb or whatever your heart desires
@roysG
Copy link

roysG commented Aug 18, 2017

Thanks!

@jiovan
Copy link

jiovan commented Aug 23, 2017

Thanks :)

@AshishDev17
Copy link

Thanks!

@iamkiet
Copy link

iamkiet commented Sep 17, 2017

thanks ;)

@CrystalCodes01
Copy link

Many thanks!

Copy link

ghost commented Nov 27, 2017

Thanks :)

@myf9000
Copy link

myf9000 commented Dec 24, 2017

Thanks :)

@aderjaan
Copy link

Yay! Thx!

@arvi
Copy link

arvi commented Jan 20, 2018

Thank you so much. I had issues with mongorestore on Mac OS High Sierra so I uninstalled installed mongodb through homebrew 😄

@james-carlson
Copy link

Thanks!

@sjtugaoman
Copy link

thanks

@mitchellsuter
Copy link

Thanks

@Llibyddap
Copy link

This was a great script and had everything in it.

@yatender-oktalk
Copy link

works like charm :) big thank you

@caidicaidi123
Copy link

Big thanks!

@meshin
Copy link

meshin commented Jul 25, 2018

Thanks!

@humbertowoody
Copy link

Thanks! :)

@salmuzblues
Copy link

Thanks, Brother.

@davens
Copy link

davens commented Dec 26, 2018

Much appreciated

@charles01
Copy link

Thank you so much.

@matthewmjm
Copy link

Thank you SO much!

@bdelville
Copy link

Thanks!
Coould also add:
rm /usr/local/etc/mongod.conf

@yuhcee
Copy link

yuhcee commented Aug 7, 2019

Thanks a bunch.

@akshithrk
Copy link

Thanks...!!

@SankalpShukla
Copy link

Hi! the unload command isn't working for me . It is showing the following error :
Unload failed: 5: Input/output error

Any help would be appreciated

@skorixperi
Copy link

Hi! the unload command isn't working for me . It is showing the following error :
Unload failed: 5: Input/output error

Any help would be appreciated

Iam getting same

@edgarberm
Copy link

MongoDB Community Edition

~/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist

@katychuang
Copy link
Author

I hadn't looked at the script file since 2014 when this gist was first created so it looks like there were some changes since then. I updated the file with some notes to provide guidance and hopefully help future proof the tips shared.

Many thanks to all who provided helpful feedback to help improve the gist.

@jim-clark
Copy link

Line 35 should be
rm -rf /usr/local/var/mongodb
However, this is where the data is stored, so the script still works, it just doesn't delete the existing data (which might be a good idea not to anyway)

@sourabhrajwade
Copy link

Thanks

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