Marcio Cunha

How to clear APT cache on Ubuntu via terminal

Learn how to free up disk space on Ubuntu by cleaning obsolete packages and old cache files stored by the APT package manager using practical commands.

Marcio Cunha•5 min
Also available in:PortuguêsEspañol
Summary
  • The APT manager stores downloaded installers locally to make future reinstalls faster without internet traffic.
  • Keeping accumulated old files consumes precious gigabytes on Linux server disks and workstations.
  • The apt-get clean command completely deletes all downloaded packages stored in the system local cache.
  • This cleanup routine can be integrated into automated scripts to prevent unexpected storage running out.
  • Other tools like Docker also generate residual files that require periodic disk space maintenance.

Why does Ubuntu accumulate old files in the APT cache?

Whenever we install or update programs in Ubuntu using the command line, the system relies on APT, the package manager responsible for searching, downloading, and installing software. To avoid excessive internet bandwidth consumption if the same program needs to be reinstalled, APT keeps a copy of each compressed installation file (with a .deb extension) inside an internal operating system folder located at var cache apt archives. In practice, this means every update you perform over months leaves behind a trail of files that keep consuming disk space indefinitely, even after the newest version is already installed and running perfectly.

Over time, this default behavior turns the cache into a graveyard of obsolete versions that will rarely be needed again. On computers with smaller SSDs or cloud virtual servers where every gigabyte counts, this accumulated fat can choke the operating system, triggering annoying low-disk-space alerts. Knowing the correct commands to tidy up this area is just as important as knowing how to manage other system resources, much like when we need to optimize tools and containers, as seen when learning how to clean Docker cache on Linux and free up space. Performing this periodic cleanup gives storage a breath of fresh air without compromising system integrity.

How to check how much space the APT cache is using

Before blindly deleting files, it is always a good practice to find out exactly how much space this folder is consuming on your drive. To do this without installing any additional programs, we can use the du command in the terminal to sum up the size of all files kept in the APT archives folder. In practice, this command scans the directory and calculates the total space in human-readable format, making it easy to quickly spot the occupied volume.

Open your terminal and run the command below to inspect the directory:

sudo du -sh /var/cache/apt/archives

If the result shows that the folder is consuming several gigabytes, it is time to perform the cleanup. If you also tend to accumulate textual data from past terminal commands, it is worth checking complementary tips on how to clear Zsh history and free up space in the terminal to keep your machine completely lean. Constant monitoring prevents the system from suddenly stopping due to a lack of space to write temporary files.

Step-by-step to completely clean the cache with apt-get clean

The most direct and radical way to empty this repository is by using the clean command. It removes absolutely all .deb files saved in the APT archives folder, except for the lock file that guarantees operational safety. It is important to highlight that this action does not uninstall any program currently running on your computer; it merely deletes the saved installers, meaning that if you need to reinstall software in the future, the system will simply download it again from the internet.

To execute this total cleanup with administrative privileges, follow these steps in the terminal:

  1. Open your terminal using the keyboard shortcut Ctrl + Alt + T.
  2. Type the complete cleanup command and press Enter:
    sudo apt-get clean
  3. Check the space occupied in the directory again to confirm the disk release:
    sudo du -sh /var/cache/apt/archives

After running these steps, you will notice that the free space on your main disk has increased considerably. This simple routine ensures that no obsolete installer sits rotting on your hard drive with no practical utility.

How to selectively free up space using apt-get autoclean

If you are afraid of deleting all files and ending up needing an old offline installer, there is an intermediate and very smart alternative called autoclean. Unlike the clean command which wipes everything out, autoclean only removes package files that can no longer be downloaded because they were replaced by newer versions in the official repositories. In practice, this means it keeps files for programs that remain current and deletes only those that have turned into historical clutter.

To run this selective and safe cleanup, use the following command in your terminal:

sudo apt-get autoclean

This surgical approach is excellent for those with unstable or limited internet connections, as it preserves useful installers while eliminating unnecessary garbage. It is a balanced strategy that saves disk space without sacrificing the convenience of having recent copies ready for local use.

Removing orphaned dependencies with the autoremove command

Often, when we install a complex program, APT pulls dozens of secondary libraries called dependencies. When we remove that main program time later, these small libraries frequently remain installed on the system, turning into orphaned packages that no longer serve any active application. Although they are not stored in the cache folder in installer format, these orphaned packages take up precious space on system partitions and deserve to be eliminated.

To scan and remove these ghost packages that are wasting space for nothing, run the automatic removal command:

sudo apt-get autoremove

This command analyzes Ubuntu's dependency tree and identifies with surgical precision everything that has lost its purpose for existence. Combining this cleanup with cache clearing ensures that your operating system remains clean, fast, and free of hardware resource waste. To explore these architectural concepts further in production, see our related technical analysis on How to test PC Power button voltage on motherboard.

Final considerations on Ubuntu preventive maintenance

Keeping Ubuntu clean and organized goes far beyond a simple matter of aesthetics or digital organization; it is a fundamental health practice for the operating system. By understanding how the APT cache works, total cleanup, and the removal of orphaned packages, you gain autonomy to manage your own computing resources without relying on heavy graphical programs. Automating or building the habit of these periodic checks prevents unexpected crashes and ensures your machine always runs at peak performance.