apt (Advanced Packaging Tool) is the package installation and dependency management tool in Debian and other Debian-based distributions. It works on top of ‘Dpkg‘ which is nothing but the Debian package installer.
The way apt installs packages is: it downloads the package for the required software, and additionally it downloads the packages for all the dependencies for the required software.
Once the packages are extracted and installation is complete, they are moved to a Cache directory which is located at ‘/var/cache/apt/archives’. Some packages for libraries, etc. are also located in other directories.
The reason why these packages are kept in the cache is that: next time another software has an existing package in the cache (with the required version), Apt will not download the package but instead use it from the Cache itself.
However as the system gets older, a lot of packages get cluttered in the Cache. Hence, it is a good practice to clear Apt Cache from time to time to free up the occupied space.
Today, we will see how to clear the Apt Cache in Debian and other Debian-based distributions.
Apt Clean Command
To delete the apt cache, we can call apt with the ‘clean‘ parameter to remove all the files in the cache directory. The user need not manually delete those files.
You can run ‘apt clean‘ with a parameter called ‘--dry-run
‘, i.e. the dry run parameter, which will simply show you the directories from which packages will be deleted, and will not actually delete the packages.
$ sudo apt clean --dry-run
To delete all these directories you can run apt clean (without dry run).
$ sudo apt clean
Apt Autoclean Command
Similar to apt clean, there is another command called ‘apt autoclean’. This command will remove the packages from Cache, for which a newer version is available in the repository.
apt will check the repository for a newer version of every package in the Cache.
$ sudo apt autoclean
The other packages in the cache, which don’t yet have a newer version in the repository, will remain in the cache and not be deleted with autoclean.
Conclusion
In this article, we learned how to clear the apt cache in Debian and Debian-based distributions. It is a good practice to clear the Cache from time to time. Users can also schedule a cron job (which automatically runs a command at specific intervals of time) to clear the Cache.
If you have any questions, let us know in the comments below!