Cleaning Package Cache

Over months of operation, a Ubuntu server accumulates downloaded package files, old kernel versions, leftover config files from removed packages, and unused dependency packages. None of this affects the running system, but it quietly consumes disk space — sometimes gigabytes on a busy server. Cleaning the package cache is a routine maintenance task.

What fills the APT cache

/var/cache/apt/archives/          ← Downloaded .deb files
├── nginx_1.24.0_amd64.deb
├── nginx_1.18.0_amd64.deb        ← Old versions kept after upgrade
└── partial/                      ← Incomplete downloads

/boot/                             ← Old kernel files
├── vmlinuz-5.15.0-100-generic    ← Old kernel
├── initrd.img-5.15.0-100-generic ← Old initramfs
├── vmlinuz-5.15.0-105-generic    ← Current kernel
└── initrd.img-5.15.0-105-generic

/var/lib/dpkg/info/               ← Package config leftovers
# Check current cache sizes before cleaning
du -sh /var/cache/apt/archives/
du -sh /boot/
df -h /

Cleaning commands

# Remove all downloaded .deb files from the cache
# (Does NOT affect any installed packages)
sudo apt clean
du -sh /var/cache/apt/archives/    # Should now be near 0

Before and after apt clean

Before: 1.2G   /var/cache/apt/archives/
After:  4.0K   /var/cache/apt/archives/
# Remove only outdated .deb files (keeps the current version of each package)
# Less aggressive than clean — useful if you want to keep recent versions cached
sudo apt autoclean

# Remove packages that were installed as dependencies and are no longer needed
sudo apt autoremove

# Remove with purge (also deletes leftover config files)
sudo apt autoremove --purge

# Combined cleanup one-liner for routine maintenance
sudo apt autoremove --purge -y && sudo apt clean

Cleaning old kernels

Each kernel update installs a new kernel and initramfs but does not automatically remove the old ones. /boot fills up over time and is often a small partition (512 MB or 1 GB on older installs).

# Check currently running kernel
uname -r

# See all installed kernel packages
dpkg -l | grep linux-image

# Check /boot disk usage
df -h /boot
ls -lh /boot/vmlinuz-*

# Remove old kernels with autoremove (safest method)
# This keeps the current kernel and the one before it
sudo apt autoremove

# If autoremove did not clean up kernels, remove them manually
# ALWAYS keep the currently running kernel (uname -r output)
sudo apt purge linux-image-5.15.0-100-generic
sudo update-grub

⚠️ WARNING: Never remove the kernel version currently running (uname -r output). If you do, the system will not be able to boot into that kernel after the next reboot. Always keep at least two kernel versions installed: the current one and the previous one, so you can boot the old kernel if the new one has a problem.

Cleaning package config leftovers

When you use apt remove instead of apt purge, config files stay on the system. These show as rc in dpkg -l:

# List packages that have been removed but left config files
dpkg -l | grep "^rc"

# Purge all of them at once
sudo apt purge $(dpkg -l | grep "^rc" | awk '{print $2}')

Example output of dpkg -l | grep "^rc"

rc  apache2      2.4.52      amd64    Apache HTTP Server
rc  php8.1-fpm   8.1.2-1     amd64    server-side PHP
rc  postfix      3.6.4       amd64    High-performance MTA

Automated cache cleanup

# Configure apt to automatically clean downloaded packages
# Add to /etc/apt/apt.conf.d/99-local-cleanup
echo 'APT::Clean-Installed "true";' | sudo tee /etc/apt/apt.conf.d/99-local-cleanup
echo 'APT::AutoRemove::RecommendsImportant "false";' | sudo tee -a /etc/apt/apt.conf.d/99-local-cleanup

# Or add a weekly cron job for cleanup
echo "0 3 * * 0 root apt-get autoremove -y && apt-get autoclean" | sudo tee /etc/cron.d/apt-cleanup

Conclusion

Regular cache cleanup on Ubuntu keeps disk usage predictable. apt clean frees downloaded package files, apt autoremove removes unused dependency packages, and purging rc-state packages removes leftover config files. Run apt autoremove after every major package removal or upgrade cycle. Clean /boot with apt autoremove when the partition fills up, but never remove the currently running kernel.

FAQ

Is Cleaning Package Cache 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