Introduction
Uninstalling MongoDB from your Windows machine is a task that you might want to undertake if you no longer need the database or if you’re looking to do a clean installation. MongoDB comes with a set of binaries that are installed in your system and might have created several data and log directories which are not removed during a standard uninstall process. This comprehensive guide explains how to completely remove MongoDB from your Windows environment.
The Steps
Step 1: Stop MongoDB Service
Before uninstalling MongoDB, you must stop any running MongoDB service. You can do this using the Windows Services Manager or through the Command Prompt.
sc stop MongoDB
This command sends a stop command to the MongoDB service. If it was successful, you will get a SUCCESS
message.
Step 2: Uninstall MongoDB
Once the service is stopped, you can proceed to uninstall MongoDB from your system via the control panel or by using commands.
msiexec /x /qb
If you installed MongoDB using the MSI installer, you need to provide the original ‘.msi’ file path. Running this command will initiate the uninstall process.
Step 3: Remove MongoDB Directory
After successfully uninstalling MongoDB, the next step is to remove any remaining MongoDB directories.
rmdir /s /q C:\Program Files\MongoDB
This command will remove the MongoDB directory from the Program Files along with all its contents.
Step 4: Delete Data and Log Directories
By default, MongoDB data and log directories are located in C:\data\db
and C:\data\log
respectively. You need to manually remove them.
rmdir /s /q C:\data\db
rmdir /s /q C:\data\log
These commands will remove the default data and log directories recursively.
Step 5: Remove MongoDB from the System Path
The MongoDB executables are usually added to the system PATH to run from the command line. You must remove the MongoDB path manually.
[System.Environment]::SetEnvironmentVariable("PATH",([System.Environment]::GetEnvironmentVariable("PATH","Machine") -replace "C:\Program Files\MongoDB\Server\4.4\bin;"),[System.EnvironmentVariableTarget]::Machine)
This PowerShell command removes the MongoDB path from the system PATH variable for all users.
Step 6: Remove MongoDB from Windows Registry
The Windows registry may contain MongoDB references that need to be cleaned out. Caution is advised when editing the registry.
reg delete "HKLM\SOFTWARE\MongoDB" /f
This command line will delete the MongoDB entry from Windows registry.
Step 7: Uninstall MongoDB Compass (Optional)
If you installed MongoDB Compass, the graphical interface for MongoDB, you might also want to uninstall it.
msiexec /x /qb
Similar to MongoDB, you need to provide the path to the ‘msi’ file to uninstall Compass.
Conclusion
Successfully removing MongoDB involves not just uninstalling the software, but also removing service details, directories, system paths, and registry entries. Following the steps outlined in this guide ensures that MongoDB is completely removed from your system, allowing for a fresh installation if necessary.