Can I safely remove /var/cache?

I am running out of disk space and noted that I have a large /var/cache directory. Can I safely remove this? (using Arch Linux, BTW).

Asked By: user11780

||

No.

For one, I believe that /var/cache/bind/ is the default directory where bind9 expects its zone files to be stored (at least on Debian; I don’t know offhand if other distros follow suit)

For another, according to this documentation, pacman (the package manager used by Arch linux) stores its package cache under /var/cache/pacman/pkg/ and it most likely expects nothing but itself to modify the contents.

I recommend you read through the documentation more closely and decide whether this is a good time to clear the package cache.

From http://www.lindevdoc.org/wiki//var/cache

Sorry for the (very) late answer, but I believe it’s important to include this bit for future reference.

Highlighted the bit which does answer this question.

The /var/cache directory contains cached files, i.e. files that were generated and can be re-generated any time, but they are worth storing to save time of recomputing them.

Any application can create a file or directory here.
It is assumed that files stored here are not critical, so the system can delete the contents of /var/cache either periodically, or when its contents get too large.

Any application should take into account that the file stored here can disappear any time, and be ready to recompute its contents (with some time penalty).

So yes, you may remove these files without expecting anything bad to happen.

Answered By: Tor Valamo

You can make this determination yourself using lsof.

Run lsof -Pn +D /var/cache/ | awk '{print $1}' | sort | uniq to see which software currently has any open files in that directory. If anything looks moderately important (or you dont know what it is) then do not remove it.

Besides, you shouldn’t be just blowing out directories without backups anyways; this even applies to /tmp. If a file is currently in use, your removal will not register until after the handler is closed (you’ll see it gone in the file system). More over, removal can cause other programs that are currently running (see the lsof command above) to crash if an expected file that’s there is missing.

Basically, only remove files that you know are safe to remove and are not currently being used by other running software.

Answered By: user26053

As others have said, /var/cache/ can be used by any application to store information to save on retrieval time. In my experience though, most of the space taken up in there is from the system’s package manager.

From the Arch Linux wiki (Manjaro also uses pacman):

pacman stores its downloaded packages in /var/cache/pacman/pkg/ and does not remove the old or uninstalled versions automatically, therefore it is necessary to deliberately clean up that folder periodically to prevent such folder to grow indefinitely in size.

pacman -Sc

However, unless storage space is a desperate issue, to save headache later from future incompatibility; one of these other tools should be utilized: paccache, pkgcacheclean, or pacleaner.


For other system:

Red Hat based (Fedora, Alma, …)

yum clean all
# or
dnf clean all

Debian based (Ubuntu, …)

apt-get autoclean

SUSE based

zypper clean
Answered By: Kevin

Found this post interesting in that I was looking to delete from /var/cache on Ubuntu 15.10 for disk space improvement, this is what I have found:

/var/cache/apt cache files are removed after ‘sudo apt-get clean’ has been run, however the directory structure remains which is no problem if you are looking for disk space improvements; ‘apt-get clean’ should be run last if you are to get any disk space improvement with apt-get [auto]remove/[auto]clean etc.

As for everything else in the directory, I couldn’t agree more with ‘Tor Valamo’ and his explanation. It’s cache, the system and apps that use cache should be able to regenerate anything they have created there. You just have minor performance overheads as cache is regenerated from app to app

In saying that, every system may not conform to this principal, do some testing, try moving everything in your cache directory to a new location and test that your system and all your applications work before removing the cache permanently.

Answered By: JeffreyJ

As far as I’ve found out, removing /var/cache (not in the middle of e.g. apt install command) only effects the following: /var/cache/man is not recreated (and w/out cache folder mandb manual run also had not recreated it) and the result is that apropos / man -k – that is search for man pages stops working.

TL;DR
I’ve tried to remove all /var (renaming it, /var_on in the example below). Here is what I’ve found so far:
I needed the following folders present at boot in /var and the following only for the system to work as before:

ln -s /var_on/lib /var/lib
ln -s /var_on/spool/cron /var/spool/cron
ln -s /var_on/cache/man /var/cache/man
ln -s /run/lock /var/lock
ln -s /run /var/run

Please comment is anything else is needed and why.

Answered By: Martian2020

I’ve run /var/cache in a tmpfs temporarily before while re-configuring a new Arch system’s mount points. Everything still worked.

To make more efficient use of disk space on a recent new system (with an nvme boot drive + 2 hard disks) – I setup a btrfs raid0 partition for variable data I don’t mind losing with an @cache subvol for /var/cache (as it recreates itself)

I also run /path/to/makepkg/sources in zram with zram-generator to save disk space / disk writes as normally you never need them again after a package is built.

Answered By: Stuart Cardall
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.