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.
Note: 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
- Sometimes you may need to free disk space and reclaim it.
- 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/
To check the disk utilized by the cache by running the following command. I
$ sudo du -sch /var/cache/apt/archives/
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
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
If you 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.