Sling Academy
Home/MongoDB/How to completely uninstall MongoDB from Mac

How to completely uninstall MongoDB from Mac

Last updated: February 01, 2024

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.

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.

Next Article: How to Completely Remove MongoDB from Ubuntu

Previous Article: How to completely remove MongoDB from Windows

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)