Uninstall MongoDB from Ubuntu

To uninstall MongoDB from Ubuntu, first stop the Mongo Daemon if it is already running, remove MongoDB packages using APT (Advanced Package Tool), and finally remove the MongoDB logs from log directory, and MongoDB Databases from library.

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

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.

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*

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

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.

Conclusion

In this MongoDB Tutorial, we have learnt to completely uninstall MongoDB from Ubuntu.