How to Completely Remove MongoDB from Ubuntu

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

Overview

Uninstalling MongoDB from Ubuntu can be necessary for various reasons, including system cleanup, re-installation, or migration to another database solution. This tutorial guides you through the process of completely removing MongoDB and its components from your Ubuntu system. We’ll start with basic commands, progress through advanced steps for removal, and conclude with some important considerations.

Prerequisites

Ensure you have administrative access to your Ubuntu machine and that you have backed up any data you wish to preserve before proceeding with the uninstallation process.

Step-by-Step Instructions

Step 1: Stop MongoDB Service

Before removing MongoDB, its services must be stopped. Use the following command:

sudo systemctl stop mongod

Step 2: Uninstall MongoDB Packages

Next, remove any installed MongoDB packages using apt-get:

sudo apt-get purge mongodb-org*

This command removes the MongoDB packages and their configuration files.

Step 3: Remove MongoDB Databases and log files

To completely remove MongoDB, including databases and logs, execute:

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

Step 4: Remove the MongoDB Repository (Optional)

If you added a MongoDB repository during installation, you might want to remove it:

sudo apt-get remove --purge mongodb-org*

Advanced Removal Steps

Following are the advanced steps to ensure complete removal:

Remove MongoDB Users

Check for any MongoDB user accounts:

getent passwd | grep mongo

Remove the MongoDB users:

sudo deluser 

Clean Up Remaining Dependencies

Remove any residual packages and dependencies that are no longer required:

sudo apt-get autoremove

Purge MongoDB from dpkg

Check all mongodb related packages

dpkg -l | grep mongo

For every package found by dpkg, purging might be needed:

sudo dpkg --purge 

Cleaning APT Cache

Freed disk space by cleaning the APT cache:

sudo apt-get clean

Verifying the Removal

Confirm that MongoDB has been removed by checking the service status:

service mongod status

If MongoDB is successfully uninstalled, you will see an error indicating there’s no such service. Also, ensure that there are no MongoDB processes running:

ps -aux | grep -v grep | grep mongodb

Conclusion

In this tutorial, we’ve gone through the process of completely removing MongoDB from an Ubuntu system. By following each step, you have ensured that MongoDB, its databases, users, and associated files are fully uninstalled, and your system is clean from any residual configurations or data.