Introduction
The apt purge command is an extended version of apt remove that deletes not only the package's executable files but also all associated configuration files. This is particularly useful when you want to completely remove a program, free up disk space, and avoid potential conflicts during reinstallation. In this guide, you will learn how to properly use apt purge and additionally clean your system of unnecessary dependencies.
Requirements / Preparation
Before you begin, ensure that:
- You are working on a Debian or Ubuntu-based system (or a derivative such as Linux Mint).
- You have superuser privileges (the ability to use
sudo). - You know the exact package name you want to remove. If you are unsure — use the search commands from the first step.
Step 1: Determine the Exact Package Name
Before removing a package, you need to find out its exact name. You can do this in two ways:
apt list --installed | grep <part_of_name>
Or:
dpkg -l | grep <part_of_name>
For example, to find all packages related to nginx, run:
apt list --installed | grep nginx
The output will contain full package names, such as nginx or nginx-common. Use the exact name from the list.
Step 2: Perform a Complete Package Removal
Now that the package name is known, remove it along with its configuration files:
sudo apt purge <package_name>
For example:
sudo apt purge nginx
The system will ask for confirmation. Enter Y and press Enter. After this, the package and its configs (files in /etc/, /var/, and others managed by the package manager) will be deleted.
Step 3: Remove Unused Dependencies
When a package is removed, dependencies that are no longer required by any installed package may remain. To clean them up, run:
sudo apt autoremove
This command will remove packages that were automatically installed as dependencies and are currently unused. Be careful: autoremove will show a list of packages before deletion. If any of them are needed, abort the operation.
Step 4: Clean the Package Cache (Optional)
APT stores downloaded package files in a cache (/var/cache/apt/archives/). Over time, this can take up significant space. To clean it:
- Delete all files from the cache:
sudo apt clean - Delete only obsolete files (those for which there is no corresponding entry in the list of available packages):
sudo apt autoclean
These actions do not affect installed packages — only local files.
Verify the Result
Ensure the package was completely removed:
- Check the list of installed packages:
apt list --installed | grep <package_name>
If the package is removed, the command will output nothing. - Manually search for leftover configuration files (usually
purgeremoves managed configs, but just in case):sudo find /etc -name "*<part_of_name>*" 2>/dev/null
If the output is empty — no configs remain. - Check disk space (optional):
df -h
Potential Issues
Package Won't Remove Due to Dependencies
If apt purge reports that the package cannot be removed because it is required by other packages, first remove the dependent package (the one that depends on the package being removed). Use apt rdepends <package_name> to see reverse dependencies.
"Unable to locate package" Error
Ensure the package name is spelled correctly and that the package was indeed installed. Check for typos.
Insufficient Permissions
All commands related to installation/removal require sudo. Run them with elevated privileges.
Leftover "Orphaned" Files
In rare cases, purge may not delete configs created manually or outside the package management system. Manually find and delete such files if they are no longer needed.