Sling Academy
Home/MongoDB/How to Completely Remove MongoDB from Ubuntu

How to Completely Remove MongoDB from Ubuntu

Last updated: February 01, 2024

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.

Next Article: How to view the disk space used by MongoDB

Previous Article: How to completely uninstall MongoDB from Mac

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)