How to clear the apt cache

When installing packages in Debian / Ubuntu with the apt-get command, a copy of the .deb file is saved locally. This helps, in case you have to reinstall them after removing them, then packages can be accessed locally (also called cache). The system does not have to go over the network for each new installation.

Downloaded packages are cached locally in / var / cache / apt / archives /. Every time you run the apt-get update and apt-get install command, the apt cache is updated.

In this tutorial we will learn how to clear the apt cache on Debian and Ubuntu systems.

Note: If you use the apt command instead of the apt-get command, this cache will be automatically cleared in the new operating system versions.


Reason to clean up the apt cache

  1. Sometimes you may need to free up and reclaim space.
  2. If you remove a package and reinstall it, the system searches the cache and you get the same version of the software.

Enter the following to list the cached .deb file:

$ ls /var/cache/apt/archives/

List of apt cache files
List of apt cache files

To check the hard drive used by the cache, run the following command. I

$ sudo du -sch /var/cache/apt/archives/

Hard disk usage by apt cache
Hard disk usage by apt cache

In my case, it’s more than 340MB.

To clean up the apt cache, use apt clean commandwhich removes everything (except the lock file) from / var / cache / apt / archives / and / var / cache / apt / archives / partial /.

You can also simulate or do a test run to see what actually happens when you clean up the apt cache, type:

$ sudo apt-get clean --dry-run

apt clean dry run
Dry running from apt clean

It shows which directory content is being removed.

It is safe to run the apt-get clean command as it will only remove a copy of the .deb files. The next time you run apt-get install, it can get packages from the mirror.

Finally closed clean up the apt cache Just run the following command.

$ sudo apt-get clean

diploma

If you just want to delete the outdated deb packages, use apt-get autoclean. This will remove package files that can no longer be downloaded and are largely useless.

In this tutorial we learned how to clean up the apt cache in Debian and Ubuntu. Your comments and suggestions are highly recommended. Thanks very much.