8. Disable APT cache to save storage space

by Cover Tower - Updated March 14, 2021
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 they want to install the same version of the package again, even without an Internet connection. In time, they can take up a lot of storage space and on a VPS or dedicated server where the Internet connection is permanent and of good quality, they are not needed, so they can be safely removed to free up valuable storage space. Storing downloaded packages in that directory can also be 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). So, run:

apt-get clean

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

cd /etc/apt/apt.conf.d

Add 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”;};

Save and exit, then add another file:

nano 00disable-cache-files

Add the following content inside:

Dir::Cache::srcpkgcache “”;

Dir::Cache::pkgcache “”;

Save and exit. 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.