How do you clear the apt cache? You simply use this apt-get command option:
sudo apt-get clean
But there is more to cleaning apt cache than just running the above command.
In this tutorial, I’ll explain what is apt cache, why is it used, why you would want to clean it and what other things you should know about purging apt cache.
I am going to use Ubuntu here for reference but since this is about apt, it is applicable to Debian and other Debian and Ubuntu-based distributions like Linux Mint, Deepin and more.
What is apt cache? Why is it used?
When you install a package using apt-get or apt command (or DEB packages in the software center), the apt package manager downloads the package and its dependencies in .deb format and keeps it in /var/cache/apt/archives folder.
While downloading, apt keeps the deb package in /var/cache/apt/archives/partial directory. When the deb package is downloaded completely, it is moved out to /var/cache/apt/archives directory.
Once the deb files for the package and its dependencies are downloaded, your system installs the package from these deb files.
Now you see the use of cache? The system needs a place to keep the package files somewhere before installing them. If you are aware of the Linux directory structure, you would understand that /var/cache is the appropriate here.
Why keep the cache after installing the package?
The downloaded deb files are not removed from the directory immediately after the installation is completed. If you remove a package and reinstall it, your system will look for the package in the cache and get it from here instead of downloading it again (as long as the package version in the cache is the same as the version in remote repository).
This is much quicker. You can try this on your own and see how long a program takes to install the first time, remove it and install it again. You can use the time command to find out how long does it take to complete a command: time sudo apt install package_name.
I couldn’t find anything concrete on the cache retention policy so I cannot say how long does Ubuntu keep the downloaded packages in the cache.
Should you clean apt cache?
It depends on you. If you are running out of disk space on root, you could clean apt cache and reclaim the disk space. It is one of the several ways to free up disk space on Ubuntu.
Check how much space the cache takes with the du command:
Sometime this could go in 100s of MB and this space could be crucial if you are running a server.
How to clean apt cache?
If you want to clear the apt cache, there is a dedicated command to do that. So don’t go about manually deleting the cache directory.
You may think it is apt-cache command but that’s deceiving. Simply use the apt-get command with clean as argument:
sudo apt-get clean
This will remove the content of the /var/cache/apt/archives directory (except the lock file). Here’s a dry run (simulation) of what the apt-get clean command deletes:
There is another command that deals with cleaning the apt cache:
sudo apt-get autoclean
Unlike clean, autoclean only removes the packages that are not possible to download from the repositories.
Suppose you installed package xyz. Its deb files remain in the cache. If there is now a new version of xyz package available in the repository, this existing xyz package in the cache is now outdated and useless. The autoclean option will delete such useless packages that cannot be downloaded anymore.
Is it safe to delete apt cache?
Yes. It is completely safe to clear the cache created by apt. It won’t negatively impact the performance of the system. Maybe if you reinstall the package it will take a bit longer to download but that’s about it.
Again, use the apt-get clean command. It is quicker and easier than manually deleting cache directory.
You may also use graphical tools like Stacer or Bleachbit for this purpose.
Conclusion
At the time of writing this article, there is no built-in option with the newer apt command. However, keeping backward compatibility, apt clean can still be run (which should be running apt-get clean underneath it). Please refer to this article to know the difference between apt and apt-get.
I hope you find this explanation about apt cache interesting. It is not something essential but knowing this little things make you more knowledgeable about your Linux system.
I welcome your feedback and suggestions in the comment section.