122

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).

3
  • 6
    I'd check through what's in there, but given that's where pacman store's it's packages you might want to run pacman -Sc or even pacman -Scc if you want to clean your entire package cache. Commented Oct 23, 2011 at 20:02
  • Most, but not all, files under /var/cache are safe to delete. Don't delete the directories or change their ownership. Run du /var/cache/* | sort -n to see what's taking room. Ask here if you don't know whether the large directories are safe to clean. Commented Oct 24, 2011 at 1:14
  • What about a disk usage analyzer - alternativeto.net/software/windirstat/?platform=linux Commented Jan 12, 2013 at 6:28

7 Answers 7

118

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.

7
  • 12
    You will have to jump through some hoops if you delete /var/cache and want to use apt afterwards. Commented Mar 6, 2014 at 21:56
  • @MatthiasBraun - For those of us not in the know, is lindevdoc.org a canonical source of truth for this sort of thing?
    – eykanal
    Commented Feb 3, 2020 at 18:52
  • I don't know lindevdoc.org (I'm not the author of the answer). The site seems to be offline. Commented Feb 4, 2020 at 1:11
  • 1
    The Wayack Machine has that page archived (but only once): web.archive.org/web/20130511220135/http://www.lindevdoc.org/… ... The original poster here literally did more work than the author of that page, as you'll see, since they at least bolded and formatted the text ;-) Commented Oct 4, 2020 at 4:36
  • 1
    I do not completely agree with this answer. Removing /var/cache from a running system may cause programs to fail. Programs may be actively using some files there, and not be ready to replace/recreate them on the fly. If you delete manually from it, you should at least be in singleuser mode
    – am70
    Commented Apr 20, 2023 at 7:25
31

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.

4
  • Yes, don't delete /var/cache/apt/archives dir, but you can delete files: /var/cache/apt/pkgcache.bin and /var/cache/apt/srcpkgcache.bin, but them will be recreated by "apt-get update".
    – diyism
    Commented Jun 24, 2013 at 12:03
  • 7
    On Debian, /var/cache/bind contains zone files transferred from other nameservers—it shouldn't contain master zones. Debian follows FHS, and FHS requires that files under /var/cache be deletable. Of course, the question is about Arch, and further one shouldn't delete files without first at least figuring out what they are.
    – derobert
    Commented Nov 12, 2013 at 22:32
  • 34
    That's like saying you shouldn't delete files in the temporary directory in windows without knowing what they are. If the folder is intended for temp files and your program puts non-temporary data there, then that's a huge problem for your program, not for /var/cache
    – Tor Valamo
    Commented Feb 18, 2015 at 1:27
  • @TorValamo Unfortunate, isn't it? The dynamic bind data and dpkg are still in that folder... plus a few other things. What's 200% temporary is under /run (a RAM disk) and what's mostly temporary under /tmp. Generally, anything under /var is data used by apps and that data may be "cache" or not really "cache". Commented Jan 28, 2022 at 5:28
24

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
3
  • 3
    +1! I noticed today that I am running out of disk space...a short du -hs /* 2> /dev/null revealed that /var was very big. Turned out that my pacman cache was 91GB. pacman -Sc cleaned up the old packages and reduced the pacman cache to 5GB!
    – daniel451
    Commented Aug 14, 2017 at 2:42
  • 2
    This article is a more verbose version of the instructions in the arch wiki on how to use paccahce Commented Mar 29, 2018 at 12:50
  • For Arch: sudo pacman -Rsn `pacman -Qqtd` is a nice command to get rid of some used space in root dir too. Commented Sep 30, 2023 at 6:42
11

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.

3

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.

0

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.

2
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. You can also add a bounty to draw more attention to this question. - From Review
    – AdminBee
    Commented Nov 4, 2021 at 11:41
  • @AdminBee, I rephrased Commented Nov 4, 2021 at 11:44
0

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.

You must log in to answer this question.