Removing Packages Safely
Removing a package on Ubuntu is not always as simple as apt remove. Some packages are dependencies for others, and removing the wrong one can pull down a chain of packages including things you need. The right approach is to check what depends on the package first, decide between remove and purge, and then clean up leftover dependencies.
remove vs purge
| Command | Removes binary? | Removes config files? | When to use |
|---|---|---|---|
apt remove nginx | Yes | No | When you might reinstall later and want to keep the config |
apt purge nginx | Yes | Yes (from /etc) | When you want a complete clean removal |
# Remove a package but keep configuration files
sudo apt remove nginx
# Remove a package AND its configuration files
sudo apt purge nginx
# Purge a package that was already removed (removes leftover config)
# dpkg -l shows "rc" status for packages removed but with config remaining
dpkg -l | grep "^rc"
sudo apt purge $(dpkg -l | grep "^rc" | awk '{print $2}')
# Verify the package is gone
dpkg -l nginx | grep -E "^ii|^rc"
dpkg status codes
ii = installed
rc = removed, config remains ← these are orphaned config files
un = not installed
Checking what depends on a package
Before removing any package that might be a shared library or common tool, check what depends on it. Removing a dependency without checking can trigger a cascade removal.
# Show what installed packages depend on nginx
apt-cache rdepends --installed nginx
# Show the full dependency chain
apt-cache depends nginx
# Simulate removal to see what else would be removed
apt-get remove --simulate nginx
Example: checking before removing libssl
$ apt-get remove --simulate libssl3
The following packages will be REMOVED:
curl git libssl3 nginx openssh-client openssh-server
If you see critical packages in the REMOVED list, do not proceed. The simulate flag shows consequences without making any actual changes.
Cleaning up unused dependencies
When you install a package, apt installs its dependencies. When you remove that package, the dependencies often remain. apt autoremove identifies and removes these orphaned dependency packages.
# See what autoremove would remove (without applying)
apt autoremove --dry-run
# Remove unused dependency packages
sudo apt autoremove
# Combine with purge to also remove their config files
sudo apt autoremove --purge
Example output
The following packages will be REMOVED:
libfcgi-bin php-common php8.3-cli php8.3-common
4 to remove. After this operation, 34.2 MB disk space will be freed.
Finding and removing orphaned packages
# Install deborphan to find truly orphaned packages
sudo apt install deborphan
# List all orphaned library packages
deborphan
# List all orphaned packages (not just libs)
deborphan --all
# Remove what deborphan finds (review the list first!)
sudo apt purge $(deborphan)
📝 NOTE:
deborphandoes not know what you actually need. Review its output before purging. Some packages are legitimately standalone and not dependencies of anything else — that does not mean you should remove them.
Removing packages without breaking the system
# Safe removal checklist:
# 1. Check if the service is running (stop it first)
systemctl status nginx
sudo systemctl stop nginx
# 2. Check what depends on the package
apt-cache rdepends --installed nginx
# 3. Simulate the removal
apt-get remove --simulate nginx
# 4. Proceed only if the simulation looks clean
sudo apt purge nginx
# 5. Clean up leftover dependencies
sudo apt autoremove
# 6. Verify the system is still functional
systemctl --failed
Conclusion
Use apt purge over apt remove for a clean removal — the difference is whether config files in /etc are deleted. Always simulate with apt-get remove --simulate before removing any package that might be a shared dependency. Run sudo apt autoremove after removal to clean up orphaned dependencies and free disk space.
FAQ
Is Removing Packages Safely important for Ubuntu administrators?+
Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.
Should I practice this on a live server?+
Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.
What should I do after reading this article?+
Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.
Need help with Ubuntu administration?
Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.
Hire Me for Support