How to completely uninstall MongoDB from Mac

Updated: February 1, 2024 By: Guest Contributor Post a comment

Overview

Uninstalling MongoDB from your Mac might be necessary if you want to reinstall it from scratch, fix a broken installation, or free up your system resources. Whatever the reason, fully removing MongoDB involves more than just dragging it to the trash. In this guide, we’ll walk you through a step-by-step process, ensuring you leave no remnants of the program on your Mac.

Caveats Before You Begin

Prior to uninstallation, please ensure that all your necessary data has been backed up. Uninstalling MongoDB will erase database files, configurations, and logs. The operations are irreversible, and data loss is possible if you haven’t created backups of important databases.

Stopping MongoDB Services

Before removing MongoDB, it’s essential to stop any running MongoDB services. Open the Terminal app and run the following commands according to the MongoDB service you have:

brew services stop mongodb-community

If you installed MongoDB without using the Homebrew package manager, you can stop the daemon process by the following approach:

mongo --eval 'db.getSiblingDB("admin").shutdownServer()'

Uninstall MongoDB Using Homebrew

If you installed MongoDB using the Homebrew package manager, uninstallation is simplified with the following commands:

brew uninstall mongodb-community
brew cleanup

The `brew cleanup` command is used to remove any leftover MongoDB formulae and versions from your system.

Manual Uninstallation Steps

If MongoDB was installed manually (i.e., not using Homebrew), you’ll need to perform these steps to properly uninstall it:

1. Remove the MongoDB Binaries

Identify where the binaries are by looking at your PATH, then remove them. Common paths include `/usr/local/bin` or `/usr/local/mongodb/bin`. Inspecting the system PATH:

echo $PATH

Removing MongoDB binaries:

sudo rm /usr/local/bin/mongod
sudo rm /usr/local/bin/mongo

2. Delete the Database and Log Files

MongoDB’s default database path is `/data/db`. However, it could be different if you explicitly set one during setup. To remove database and log files:

sudo rm -rf /data/db
sudo rm -rf /var/log/mongodb

Ensure to substitute `/data/db` with the correct path if your database location is different.

3. Remove Configuration Files

Delete the MongoDB configuration file, which may be located at one of the following locations:

sudo rm /usr/local/etc/mongod.conf
sudo rm /etc/mongod.conf

Again, your configuration file could be in a different directory, so adjust the command accordingly.

Advanced Removal Steps: Locate and Delete Orphans

Some configurations and installations might leave orphan files on your system. To find and remove these files, use the `find` command:

sudo find / -name 'mongodb*' -exec rm -rf {} +

This command searches the entire file system for any files or directories that contain ‘mongodb’ in their name and deletes them. Be cautious with this command, as it has the potential to delete non-MongoDB related files if they match the pattern.

Cleaning Up User and System Plists

Older versions of MongoDB or manual installations may have left behind Property List files, known as plists, which you should also remove:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist
rm ~/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist
sudo rm /Library/LaunchDaemons/homebrew.mxcl.mongodb-community.plist
sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.mongodb-community.plist

After removing the plists, ensure that you also unload them via the `launchctl` command as shown above.

Zapping MongoDB Related User Preferences

Finalize the removal process by clearing out leftover user preferences:

defaults delete /Library/Preferences/com.mongodb.plist

Conclusion

This thorough un-installation process will completely free your system of MongoDB and its associated data. Whether you’re clearing the way for a fresh install or freeing up system resources, following this guide ensures that MongoDB and all its traces are successfully removed from your Mac.