Sub topic 11
Cleaning Up with autoremove and Cache Cleaning
This chapter assumes you can use a shell, sudo, and either apt on Debian or Ubuntu, or dnf on a RHEL-family system. It also assumes you have already completed a patching transaction and want to remove files that are no longer useful.
Why cleanup needs review
Package managers keep two kinds of leftovers:
- Unused dependencies are packages installed automatically to support another package. When that package is removed, the dependency may no longer be needed.
- Package caches are downloaded package files stored locally. They can help with reinstalling, but they consume disk space.
Cleanup reduces disk usage, but it can also remove software you still need. Always preview an autoremove operation before accepting it. Cache cleanup is usually lower risk because the package manager can download the files again.
Remove unused dependencies
On Debian and Ubuntu, apt autoremove proposes packages that were installed automatically and are no longer required by installed packages.
sudo apt autoremove --simulate
sudo apt autoremoveThe simulation makes no changes. If the list contains an important package, stop and investigate why it is marked as automatic or unused. The apt-mark command can change that status:
apt-mark manual package-nameMarking a package as manual tells apt that you intentionally need it. Replace package-name with the real package name. Do not mark a package manually just to hide an unexplained dependency problem.
On RHEL-family systems, dnf autoremove finds dependencies that are no longer needed:
sudo dnf autoremove --assumeno
sudo dnf autoremove--assumeno answers no automatically, so you can inspect the transaction before making changes. Dnf may list packages that were installed as dependencies for software you have since removed.
| Cleanup action | Debian/Ubuntu | RHEL-family |
|---|---|---|
| Preview unused dependency removal | `sudo apt autoremove --simulate` | `sudo dnf autoremove --assumeno` |
| Remove unused dependencies | `sudo apt autoremove` | `sudo dnf autoremove` |
Clean package caches
Apt stores downloaded package files under /var/cache/apt/archives.
apt autocleanremoves cache files that can no longer be downloaded from configured repositories.apt cleanremoves all cached package files.
sudo apt autoclean
sudo apt cleanUse autoclean when you want a conservative cleanup. Use clean when disk space is more important than keeping cached packages for a possible reinstall.
Dnf stores downloaded packages and metadata in its cache. This command removes all cached data:
sudo dnf clean allCleaning the cache does not remove installed software. The next metadata refresh or package installation downloads what it needs again.
Verify after cleanup
Check free space before and after cleanup:
df -h /Record the package-manager output in your maintenance log. Then verify that important services still run:
systemctl --failed
systemctl status ssh --no-pagerReplace ssh with a service that matters on your server. Cleanup should be a separate, reviewable step after patching, not an automatic response to every update.
Exercise
You patched an Ubuntu server and need to free space, but you want the least aggressive cleanup. Which commands should you use first?
Show answer
Run sudo apt autoremove --simulate to review unused dependencies, then use sudo apt autoclean to remove obsolete cached package files. Run the real autoremove only after checking its proposed package list.