121

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

  • 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. – xenoterracide Oct 23 '11 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. – Gilles 'SO- stop being evil' Oct 24 '11 at 01:14
  • What about a disk usage analyzer - http://alternativeto.net/software/windirstat/?platform=linux – Aaron Newton Jan 12 '13 at 06:28

7 Answers7

116

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.

Tor Valamo
  • 1,261
  • 12
    You will have to jump through some hoops if you delete /var/cache and want to use apt afterwards. – Matthias Braun Mar 06 '14 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 Feb 03 '20 at 18:52
  • I don't know lindevdoc.org (I'm not the author of the answer). The site seems to be offline. – Matthias Braun Feb 04 '20 at 01:11
  • The Wayack Machine has that page archived (but only once): http://web.archive.org/web/20130511220135/http://www.lindevdoc.org/wiki//var/cache ... 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 ;-) – Brent Rittenhouse Oct 04 '20 at 04:36
  • @Brent, IMO format here is better (separate paragraphs). – Martian2020 Oct 27 '21 at 22:46
  • On my Linux man pages' search (apropos) stopped working. – Martian2020 Oct 27 '21 at 22:47
  • 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 Apr 20 '23 at 07: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.

  • 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 Jun 24 '13 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 Nov 12 '13 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 Feb 18 '15 at 01: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". – Alexis Wilke Jan 28 '22 at 05:28
23

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:

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:

Redhat based (Fedora, CentOS, SL, ...)

yum clean all
# or
dnf clean all

Debian based (Ubuntu, ...)

apt-get autoclean

SUSE based

zypper clean
Kevin
  • 540
  • 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 Aug 14 '17 at 02:42
  • 2
    This article is a more verbose version of the instructions in the arch wiki on how to use paccahce – joelostblom Mar 29 '18 at 12:50
  • For Arch: [sudo pacman -Rsn \pacman -Qqtd``](https://bbs.archlinux.org/viewtopic.php?pid=1058765#p1058765) is a nice command to get rid of some used space in root dir too. – Giorgos Xou Sep 30 '23 at 06: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.

JeffreyJ
  • 111
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.

Martian2020
  • 1,167
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.