Uninstall MongoDB from Ubuntu

To uninstall MongoDB from Ubuntu, stop the MongoDB service first, purge the installed MongoDB packages with APT, and then remove the default MongoDB log and data directories. This is the usual complete removal flow for MongoDB Community Edition packages installed on Ubuntu.

Before you run the removal commands, decide whether you want to keep a backup of your databases or configuration. Removing /var/lib/mongodb deletes the local database files, and removing /var/log/mongodb deletes MongoDB logs. If you only want to reinstall MongoDB while keeping your data, do not remove the data directory.

Following are the list of commands that have to executed in order to uninstall MongoDB from Ubuntu.

sudo service mongod stop
sudo apt-get purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

The commands above match the common mongodb-org package installation. Some older Ubuntu installations may use package or service names such as mongodb, mongodb-server, mongodb-clients, mongo-tools, or mongosh. The sections below show how to check those cases without changing the basic uninstall order.

Check the MongoDB Service and Installed Packages on Ubuntu

First, check whether Ubuntu knows the service as mongod or mongodb. MongoDB packages from the official MongoDB repository normally use mongod, while older distribution packages may use different names.

</>
Copy
systemctl status mongod
systemctl status mongodb
dpkg -l | grep -E 'mongodb|mongo-tools|mongosh'

If a status command says that the unit is not found, it simply means that service name is not present on your system. Continue with the package removal step for the package names that are actually installed.

Step-by-Step MongoDB Removal from Ubuntu

Following is a detailed step by step process to uninstall MongoDB. Open a Terminal and follow these steps one by one sequentially.

1. Stop Mongo Daemon

We have to stop Mongo Daemon if it is already running in the system.

Run the following command in a Terminal.

sudo service mongod stop

Stopping Mongo Daemon ensures that there are no locks on any database files or such.

On systems that use systemd, the equivalent command is usually:

</>
Copy
sudo systemctl stop mongod

If your installation uses the older mongodb service name, stop that service instead.

</>
Copy
sudo systemctl stop mongodb

2. Remove MongoDB Packages

Now, we have to remove MongoDB packages. We will use apt command to purge MongoDB packages.

Running the following command removes all the installed packages for MongoDB.

sudo apt-get purge mongodb-org*

If dpkg -l shows additional MongoDB-related packages, remove only the packages that are present on your system. For example, older installations or installations with separate MongoDB tools may need one or more of the following package names.

</>
Copy
sudo apt-get purge mongodb mongodb-server mongodb-clients mongodb-dev
sudo apt-get purge mongo-tools mongodb-mongosh mongodb-database-tools

After purging packages, you can remove unused dependencies that were installed only for MongoDB.

</>
Copy
sudo apt-get autoremove

3. Remove MongoDB Logs

Any logs for MongoDB, by default, is written to the location /var/log/mongodb/. Remove /var/log/mongodb/ and its sub-directories recursively.

sudo rm -r /var/log/mongodb

If the directory does not exist, Ubuntu may show a “No such file or directory” message. That is not a problem; it only means the default MongoDB log directory has already been removed or was never created.

4. Remove MongoDB Databases

Any databases created in MongoDB, by default, is written to the location /var/lib/mongodb/. Remove /var/lib/mongodb/ and its sub-directories recursively.

sudo rm -r /var/lib/mongodb

If you have created any other directories for MongoDB databases, you may also remove them.

To find a custom database path before deleting data, check the storage.dbPath value in your MongoDB configuration file.

</>
Copy
grep -n 'dbPath' /etc/mongod.conf

If dbPath points to a custom location, remove that directory only after confirming that you no longer need the databases stored there.

Remove the MongoDB APT Repository from Ubuntu

If you installed MongoDB from the official MongoDB APT repository and do not plan to reinstall it, you can also remove the MongoDB repository list file. This prevents apt update from continuing to check MongoDB repository entries.

</>
Copy
ls /etc/apt/sources.list.d/ | grep mongodb
sudo rm /etc/apt/sources.list.d/mongodb-org-*.list
sudo apt-get update

If the ls command does not show a MongoDB repository file, skip this step. Repository file names can differ based on the MongoDB version and Ubuntu release used during installation.

Verify That MongoDB Has Been Uninstalled from Ubuntu

After removing the service, packages, logs, and data directory, verify that no MongoDB server package remains installed.

</>
Copy
dpkg -l | grep -E 'mongodb|mongo-tools|mongosh'
which mongod
which mongosh

If these commands still show MongoDB packages or binaries, remove the remaining package shown by dpkg. If mongosh still exists, it may be installed as a separate shell package and can be removed separately with APT.

Common Ubuntu MongoDB Uninstall Problems

“Unit mongod.service not found” while stopping MongoDB

This usually means the mongod service is not installed under that name. Try checking mongodb as the service name, or continue to the package purge step if MongoDB is not running.

apt says MongoDB packages are not installed

This is common when MongoDB was already removed or was installed with different package names. Use dpkg -l | grep -E 'mongodb|mongo' to identify the exact package names before purging anything else.

mongosh is still available after uninstalling MongoDB

The MongoDB Shell can be installed as a separate package. If you do not need it, remove mongodb-mongosh. MongoDB Database Tools can also be installed separately, so check for mongodb-database-tools or mongo-tools if tools such as mongodump or mongoexport remain.

FAQ on Uninstalling MongoDB from Ubuntu

Does uninstalling MongoDB from Ubuntu delete my databases?

Purging MongoDB packages removes the software and configuration files, but deleting /var/lib/mongodb removes the default local database files. If you need the data later, back it up before running the remove command for the data directory.

Should I use apt remove or apt purge for MongoDB?

Use apt purge when you want to remove MongoDB packages along with package configuration files. Use apt remove only when you want to remove the package binaries but keep configuration files for possible reuse.

Why does mongod still show after uninstalling MongoDB?

It may be left from another package, a manual installation, or a binary in a directory that is still in your PATH. Check which mongod and dpkg -l | grep mongo to find where it is coming from before deleting files manually.

Can I reinstall MongoDB after removing it from Ubuntu?

Yes. You can reinstall MongoDB later using the installation steps for your Ubuntu version. If you deleted /var/lib/mongodb, the previous local databases will not be available unless you restored them from a backup.

Editorial QA Checklist for This Ubuntu MongoDB Uninstall Guide

  • Confirm that the guide clearly warns readers before deleting /var/lib/mongodb.
  • Keep the uninstall order as service stop, package purge, log removal, and data removal.
  • Include both common service-name possibilities: mongod and mongodb.
  • Do not suggest deleting a custom dbPath without asking the reader to verify the path first.
  • Check that every new terminal command block uses the language-bash PrismJS class.

Conclusion: Complete MongoDB Removal from Ubuntu

In this MongoDB Tutorial, we have learnt to completely uninstall MongoDB from Ubuntu by stopping the service, purging MongoDB packages, removing logs, and deleting the default data directory when the databases are no longer required.