How to clear Apt cache on Ubuntu and free disk
Learn how system administrators can remove obsolete APT packages and quickly reclaim gigabytes of storage space on Linux.
Summary
- The APT package manager stores temporary files in the var cache apt archives directory every time a software is installed or updated.
- The apt-get clean command completely deletes all downloaded package files, freeing up the maximum amount of storage space on the disk.
- The apt-get autoclean routine removes only older packages that can no longer be downloaded again, preserving still useful files.
- Using apt autoremove complements the cleanup by eliminating orphan dependencies left behind by previously uninstalled programs.
- Automating these cleanups through periodic scripts prevents system partitions from filling up and causing operational instabilities.
Why the APT cache consumes so much space on Linux
Anyone managing servers or workstations based on Debian and Ubuntu usually runs into a common problem after a few months of use: the operating system warns that disk space is running low. Most of the time, the main culprit behind this silent consumption is the local package repository, managed by a tool called APT. In practice, every time you update the system or install a new program, the computer downloads compressed installation files, known as .deb files, and stores them in an internal folder just in case it needs them again in the future. Over time, hundreds of gigabytes of old updates accumulate unnoticed, occupying precious sectors of the hard drive. To better understand how to optimize storage in your environment, it is also worth checking out complementary tips on how to free disk space in Docker via CLI when containers start bloating the system.
How to check the current size of the package cache
Before blindly deleting files from the filesystem, the first recommended step for any prudent system administrator is to measure the exact size of the problem. Linux offers fast native commands to inspect the space occupied by specific directories without complications. In practice, the du command checks the disk usage of the folder where APT stores downloaded packages, displaying the result in a human-readable format. This initial diagnosis helps gauge the space gain you will achieve right after executing the cleanup procedures. To perform this quick check on your terminal command line, use the instruction below with appropriate privileges.
du -sh /var/cache/apt/archivesHow to wipe everything using the clean command
If your top priority is reclaiming the absolute maximum amount of disk space in a few seconds, the most drastic and efficient tool available is the clean command. In practice, it acts like a general cleanup that completely empties the APT temporary files folder, deleting all accumulated packages whether they are old or recent versions. It is worth noting that this action does not uninstall any software currently running on your operating system; it simply discards the saved installers that have already served their primary purpose. Should you need to reinstall a package in the future, the system will simply download it from the internet again automatically. To perform this deep cleaning safely in the terminal, run the command with administrative permissions.
sudo apt-get cleanHow to remove obsolete packages with autoclean
For administrators who prefer a more conservative and surgical approach, there is a very interesting intermediate alternative called autoclean. In practice, while the clean command deletes absolutely everything without distinction, autoclean analyzes the local repository and removes only package files that have been replaced by newer versions and can therefore no longer be downloaded from official servers. This means that files for programs that are still up to date on your disk are safely preserved, allowing for offline reinstalls if necessary. This strategy perfectly balances disk space savings with the preservation of useful files for occasional emergency maintenance. Type the command below in your terminal to apply this selective cleanup.
sudo apt-get autocleanHow to eliminate orphan dependencies with autoremove
Often, just cleaning the compressed files stored in the cache is not enough to restore operating system health. When we uninstall a large program, it is common for it to have brought along dozens of libraries and auxiliary packages called dependencies, which end up forgotten and abandoned in the system. In practice, these orphan packages continue to occupy disk space and consume metadata, even without any active application making use of them. The autoremove command was created precisely to sweep the system for these useless traces and eliminate them in one go, complementing the APT cleanup. Execute the following instruction to purge these ghost dependencies from your environment. To complement this implementation in production environments, see our related technical analyses on How to test electromechanical relays with a multimeter and How to inspect network routes with the mtr command on Linux.
sudo apt-get autoremove --purgeFinal thoughts on preventive disk maintenance
Keeping the operating system clean and with adequate free space is not just a matter of technical aesthetics, but a fundamental requirement to ensure the stability and performance of servers and workstations. By incorporating regular routines to clean the APT cache and remove obsolete packages, you avoid unpleasant surprises with full partitions during critical production moments. Furthermore, combining these practices with other preventive maintenance tasks—such as log management and resource monitoring—ensures a much more resilient and predictable IT infrastructure in the long run.