0

Every time I need to clear cache, I have to go inside each folder and delete the contents. Doing it via terminal deletes the whole folder which is not recommended. What shall I do?

  • 1
    What would be the problem with deleting the folders? – Joakim Danielson Apr 18 at 5:50
  • one can use os and sys python libraries and write a recursive code with *.file *.plist and all other extensions in the cache folder. Also tools like dr cleaner might work. if you need to reply to me please tag me, SE doesn't give notifs if i am not tagged. – ankiiiiiii Apr 18 at 6:01
  • 2
    Why do you need to clear cache and which caches? – Mark Apr 18 at 8:24
  • 1
    My favorite program to "clean" macOS (Caches and the like) is titanium-software.fr/en/onyx.html. Been using it for years, works great. Different version for each macOS version tho. – Steve Chambers Apr 18 at 13:31
  • I’m not going to cast a binding close vote, but this will need specific detail what a cache file is. And which folder and which command line command is used. There are some useful answers, so be sure to vote and support them and not blame them for a question that could stand some clarification. Please and thanks for being kind and constructive. – bmike Apr 19 at 1:05
2

For so many questions like this, I feel folks are answering without asking what the poster's intention is.

Why do you want to delete the cache files automatically? It's generally a bad idea to do so.

I've seen so many questions posted where folks want to delete some system application or cache or configuration file or whatever. This just leads to the point where the OS must be reinstalled or the user complains that the machine is unstable/unreliable and someone must spend hours, days or months trying to diagnose something that never should have been tampered with.

macOS, in particular, is NOT Windows. You do NOT need to purge caches, defragment or whatever. If you're doing that, especially if you're doing that on a regular basis, there's a deeper problem.

I'm writing this response on a MacBook Pro that has not had any explicit maintenance performed since El Capitan; I'm now running the latest Mojave having upgraded dozens of times. I do software development, generally have a ton of Docker containers and Parallels VM's going and do a lot of video editing with FCP X/Motion. I have had extremely few problems and zero crashes across several years of use. Performance is wonderful.

Part of this is because I don't wake up in the morning and, on a whim, decide to delete my cache files, manually edit my partition table or delete key services like Safari.

As I write this response, I see posts about things like "Firefox freezes when I delete cache files and cookies" and "How do I restore an accidentally deleted key chain". Call me stupid AF, but why on Earth are you tempting fate?

All software makes many assumptions about its underpinnings. Nobody can write code that accounts for every possibility. I know I'm deep in rant territory, but you shouldn't be deleting cache files unless you're intimately familiar every line of code that uses those cache files, directly or indirectly. I assure you that neither Apple nor Microsoft have tested that their software functions correctly if some or all of the system cache files disappear...in fact, having an inconsistent set of cache files because you deleted some of them but didn't even know about all of them is precisely how you get into trouble.

And if you still feel the need to muck around under the hood, PLEASE back up religiously. One day you're going to goof and do a "sudo rm -rf /" or create some inconsistency that can't easily be fixed.

Just consider...Humpty Dumpty was pushed...

  • 1
    Can't agree more. While cache files are temporary and most programs are able to start again, apps actually need those files and deleting them needlessly increases wear and tear and reduces performance, as all the apps need to rebuild them before all works normally again. Cache files should only be deleted when there is an issue, like an app crashing when it's started, and even then ideally only that one's files and maybe what you see from long deleted apps and such. – Carl Dombrowski Apr 18 at 23:47
0

Method #1: Using find

You can use standard UNIX CLI tools to do this from the terminal quite easily. The standard tool for finding files and directories is ironically called find.

For eg. here I'm using it to find all .gz files under /:

$ sudo find /Library/Caches/ -type f -name '*.gz' | head
/Library/Caches//WorldWindData/Earth/PlaceNames/WFSPlaceNamesVersion1.0/toppwpl_oceans/0/0_0.xml.gz
/Library/Caches//WorldWindData/Earth/PlaceNames/WFSPlaceNamesVersion1.0/toppwpl_waterbodies/0/0_2.xml.gz
/Library/Caches//WorldWindData/Earth/PlaceNames/WFSPlaceNamesVersion1.0/toppwpl_waterbodies/0/0_0.xml.gz
/Library/Caches//WorldWindData/Earth/PlaceNames/WFSPlaceNamesVersion1.0/toppwpl_waterbodies/0/0_4.xml.gz
/Library/Caches//WorldWindData/Earth/PlaceNames/WFSPlaceNamesVersion1.0/toppwpl_waterbodies/0/0_3.xml.gz
/Library/Caches//WorldWindData/Earth/PlaceNames/WFSPlaceNamesVersion1.0/toppwpl_waterbodies/0/0_1.xml.gz
/Library/Caches//WorldWindData/Earth/PlaceNames/WFSPlaceNamesVersion1.0/toppwpl_waterbodies/0/0_5.xml.gz
/Library/Caches//WorldWindData/Earth/PlaceNames/WFSPlaceNamesVersion1.0/toppwpl_waterbodies/0/0_7.xml.gz
/Library/Caches//WorldWindData/Earth/PlaceNames/WFSPlaceNamesVersion1.0/toppwpl_waterbodies/1/1_2.xml.gz
/Library/Caches//WorldWindData/Earth/PlaceNames/WFSPlaceNamesVersion1.0/toppwpl_waterbodies/1/1_0.xml.gz

You can then build a find that will locate your files and then tell find to delete them like so:

$ find /path/to/some/dir -type f -name '*.gz' -delete

Example

Here I'm making a sample directory called somedir that will contain a bunch of fake .plist files in it just to show how the above command can work.

$ mkdir dir{1..10}
$ touch dir{1..10}/file{1..10}.gz file{1..10}.gz

This will result in sub directories named dir1, dir2, ..., dir10 with files both inside and outside names file1.gz, ..., file10.gz.

Some of the structure:

$ tree  | head -20
.
├── dir1
│   ├── file1.gz
│   ├── file10.gz
│   ├── file2.gz
│   ├── file3.gz
│   ├── file4.gz
│   ├── file5.gz
│   ├── file6.gz
│   ├── file7.gz
│   ├── file8.gz
│   └── file9.gz
├── dir10
│   ├── file1.gz
│   ├── file10.gz
│   ├── file2.gz
│   ├── file3.gz
│   ├── file4.gz
│   ├── file5.gz
│   ├── file6.gz
...
...

Now to delete all the .gz files:

$ find . -type f -name '*.gz' -delete

Resulting in this:

$ tree
.
├── dir1
├── dir10
├── dir2
├── dir3
├── dir4
├── dir5
├── dir6
├── dir7
├── dir8
└── dir9

10 directories, 0 files

More files

If you need to delete more than just .gz files you can include more -name... arguments to specify them.

$ find . -type f -name '*.gz' -o -name '*.blah'

Here we're looking for files that match the pattern .gz or .blah. The -o is a logical OR operator to find.

Method #2: Using AssetCacheManagerUtil

If your system is making use of Caching content you can use the CLI tool AssetCacheManagerUtil to perform a flushCache. In case you're not familiar with this functionality within macOS:

Content caching is a macOS service that helps reduce Internet data usage and speed up software installation on Mac computers, iOS devices, and Apple TV.

Content caching speeds up downloading of software distributed by Apple and data that users store in iCloud by saving content that local Mac computers, iOS devices, and Apple TV devices have already downloaded. The saved content is stored in a content cache on a Mac, and is available for other devices to retrieve without going out over the Internet.

This CLI has a man page that explains how to operate it.

$ man AssetCacheManagerUtil

You can use this command to see if it's active already:

$ AssetCacheManagerUtil status

To flush cached content you'd use this command:

$ sudo AssetCacheManagerUtil flushCache

2019-04-18 20:25:25.510 AssetCacheManagerUtil[70975:1826791] Content caching flushed its cache.

To flush your personal cached content:

$ sudo AssetCacheManagerUtil flushPersonalCache

2019-04-18 20:26:03.147 AssetCacheManagerUtil[71033:1827060] Content caching flushed its cache.

You can read more about macOS's Caching Service here:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .