8. Disable the APT cache to save storage space

by Double Bastion - Updated January 27, 2022

By default, when using apt-get to install or upgrade a software package, a copy of the downloaded deb file is saved in /var/cache/apt/archives, so that the administrator can use it in the future, if he wants to install the same version of the package again, even without an Internet connection. In time, these files can take up a lot of storage space and on a server where the Internet connection is permanent and of good quality, they are not needed. Therefore, they can be safely removed to free up valuable storage space. Storing downloaded packages in that directory can be also disabled. The apt-get clean command removes all packages from the package cache, that is from /var/cache/apt/archives and /var/cache/apt/archives/partial. It only leaves the /var/cache/apt/archives/lock file in place. (It also removes the /var/cache/apt/pkgcache.bin and /var/cache/apt/srcpkgcache.bin files). Therefore, run:

apt-get clean

Then navigate to /etc/apt/apt.conf.d:

cd /etc/apt/apt.conf.d

Create a file called 00clean-cache-dir:

nano 00clean-cache-dir

Add the following content inside this file:

DPkg::Post-Invoke {"/bin/rm -f /var/cache/apt/archives/*.deb || true";};

Then create another file:

nano 00disable-cache-files

Add the following content inside:

Dir::Cache::srcpkgcache "";
Dir::Cache::pkgcache "";

From now on, when you install a package with apt-get, the archived package will be immediately deleted from /var/cache/apt/archives . The cache files /var/cache/apt/pkgcache.bin and /var/cache/apt/srcpkgcache.bin will also disappear. This will save a lot of storage space.

You can send your questions and comments to: