How to Clear Apt Cache

Written by: Linuxopsys   |   Last updated: July 5, 2022

When you install packages in Debian/Ubuntu using the apt-get command, a copy of the .deb file is locally stored. This helps in case you need to reinstall them after removal, then packages can be accessed locally (in another way called cache). Each time when you reinstall, the system doesn't need to go through the network.

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

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

If you are using apt command, instead of apt-get command then this cache is automatically deleted in the new OS versions.

Reason to clean Apt cache

  1. Sometimes you may need to free disk space and reclaim it.
  2. Removing a package and reinstalling it, the system will look on the cache and you will get the same version of the software.

To list .deb file which stored in cache, type:

$ ls /var/cache/apt/archives/
List of apt cache files
List of apt cache files

To check the disk utilized by the cache by running the following command. I

sudo du -sch /var/cache/apt/archives/
Disk utilization by apt cache
Disk utilization by apt cache

In my case, it is more than 340 MB.

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

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

sudo apt-get clean --dry-run
Dry run of apt clean
Dry run of apt clean

It display which directory content will be removed.

Its safe to run apt-get clean command as its only removes a copy of .deb files. Next time when you run apt-get install it can fetch packages from mirror.

Finally to clean the apt cache simply run following command.

sudo apt-get clean

Conclusion

If your intention is to clear only the obsolete deb packages, use apt-get autoclean. This will removes package files that can no longer be downloaded, and are largely useless.

In this tutorial, we learned how to clean the apt cache in Debian and Ubuntu. Your comments and suggestions are highly recommended. Thank you.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment