How to clear the APT cache

Clear the APT cache to reclaim disk space used by the downloaded packages.

Inspect APT cache configuration

Check out the current APT cache configuration using the following command.

$ apt-config dump | grep "^Dir\( \|::Ca\)"
Dir "/";
Dir::Cache "var/cache/apt";
Dir::Cache::archives "archives/";
Dir::Cache::srcpkgcache "srcpkgcache.bin";
Dir::Cache::pkgcache "pkgcache.bin";
Dir::Cache::Backup "backup/";

Remove every package from the cache

Perform a simulation of the whole process.

$ sudo apt-get clean --dry-run
Del /var/cache/apt/archives/* /var/cache/apt/archives/partial/*
Del /var/lib/apt/lists/partial/*
Del /var/cache/apt/pkgcache.bin /var/cache/apt/srcpkgcache.bin

Remove retrieved package files.

$ sudo apt-get clean

Remove outdated packages from the cache

Perform a simulation of the whole process.

$ sudo apt-get autoclean --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Del mc-data 3:4.8.18-1 [1,267 kB]
Del mc 3:4.8.18-1 [513 kB]

Remove retrieved package files that can no longer be downloaded.

$ sudo apt-get autoclean

You can use APT::Clean-Installed configuration option to keep or remove outdated and installed packages from cache.

In the following example the mc package is installed, but cannot be downloaded any longer.

$ apt-cache policy mc
mc:
  Installed: 3:4.8.18-1
  Candidate: 3:4.8.18-1
  Version table:
 *** 3:4.8.18-1 100
        100 /var/lib/dpkg/status
$ apt-cache policy mc-data
mc-data:
  Installed: 3:4.8.18-1
  Candidate: 3:4.8.18-1
  Version table:
 *** 3:4.8.18-1 100
        100 /var/lib/dpkg/status

When APT::Clean-Installed is set to false then outdated mc package that is no longer available in remote repository will not be removed from cache as it is installed on the system.

$ sudo apt-get -o APT::Clean-Installed=false autoclean --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done

When APT::Clean-Installed is set to true then outdated mc package that is no longer available in remote repository will be removed from cache as there are no restrictions imposed on these packages.

$ sudo apt-get -o APT::Clean-Installed=true autoclean --dry-run
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Del mc-data 3:4.8.18-1 [1,267 kB]
Del mc 3:4.8.18-1 [513 kB]

Permanently define true or false value.

$ echo -e 'APT::Clean-Installed true;' | sudo tee /etc/apt/apt.conf.d/80_autoclean_installed
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu