
MongoDB leaves its database files, logs and apt repository behind when you remove the packages. Follow these steps to remove it completely from Ubuntu.
Warning: deleting /var/lib/mongodb destroys every database stored on this server. If you may need the data, back it up first:
mongodump --out ~/mongo-backup-$(date +%F)
1. Stop the service
sudo systemctl stop mongod sudo systemctl disable mongod
On very old installs from Ubuntu's own mongodb package, the service is called mongodb instead of mongod.
2. Remove the packages
sudo apt-get purge -y "mongodb-org*" sudo apt-get autoremove -y
If you installed Ubuntu's own package, use sudo apt-get purge -y mongodb mongodb-clients mongodb-server instead.
3. Delete the data and log directories
sudo rm -r /var/log/mongodb sudo rm -r /var/lib/mongodb
If you set a custom dbPath in /etc/mongod.conf, delete that directory too.
4. Remove the MongoDB apt repository
sudo rm /etc/apt/sources.list.d/mongodb-org-*.list sudo rm /usr/share/keyrings/mongodb-server-*.gpg sudo apt-get update
5. Remove the mongodb user (optional)
sudo userdel mongodb
6. Confirm
which mongod mongod --version dpkg -l | grep -i mongo
All three should return nothing or report that the command is not found.
Troubleshooting
- Process still running: run
ps aux | grep mongoand stop it withsudo kill <PID>. - Port 27017 still busy: check with
sudo lsof -i :27017. - Using MongoDB Atlas: uninstalling the local server does not affect your cloud databases.
Covering financial markets, commodity rates, and practical technology insights for India. Researched and fact-checked in accordance with our Editorial Policy.
Related & Trending Stories
Recommended based on your reading interest

