apt autoremove deletes packages APT considers unused. On servers with manually installed dependencies, review the list before confirming — autoremove is not magic, it follows dependency graphs only.
What you will achieve
Reclaim disk space after upgrades by removing orphaned packages and clearing stale package cache — without breaking metapackages that still pull in required components.
1) See what autoremove would delete
Simulate first. No surprises, no tears.
sudo apt autoremove --dry-run
The list shows packages installed only as dependencies of removed packages. If you see something you still need (a library for a compiled app, an old kernel module package), install it explicitly before autoremoving:
sudo apt install package-name
2) Run autoremove
sudo apt autoremove
After a dist-upgrade or removing a large metapackage (desktop environment, old kernel metapackage), this often frees hundreds of megabytes.
3) Clean the package cache
Downloaded .deb files accumulate in /var/cache/apt/archives/:
sudo apt autoclean # removes superseded packages
sudo apt clean # removes all cached debs
autoclean is conservative; clean wipes the entire cache. Both are safe for system operation — APT re-downloads on next install.
4) Old kernels on Ubuntu
Ubuntu keeps several kernels; autoremove prunes old ones when a newer one is installed and working. Verify your running kernel first:
uname -r
dpkg -l 'linux-image-*' | grep ii
Keep at least one previous kernel until you have boot-tested the latest. Do not manually remove the running kernel's packages.
5) Orphan vs manually installed
apt-mark showauto
apt-mark showmanual
Mark a package manual if autoremove keeps threatening it:
sudo apt-mark manual libfoo-dev
6) Full cleanup workflow after upgrade
sudo apt update
sudo apt upgrade
sudo apt autoremove
sudo apt autoclean
7) When autoremove refuses
If a package is both auto-installed and still required by a running service, APT keeps it. Run sudo apt rdepends packagename to see reverse dependencies. On production, snapshot /etc/apt/ before aggressive cleanup — restoring package state is easier with backups.
Verify
df -h /var
dpkg -l | grep ^rc
Check free space improved. Residual rc rows are removed configs — purge with sudo dpkg --purge packagename if you want those gone too.