Contents of directory ~/.cache/Arduino/sketches, can it be deleted

Hi folks,

a question with regards the contents of the aforementioned directory (Linux).
Does anyone know whether the contents of this directory can be purged after a sketch has been compiled and uploaded?

I'm thinking about a script scheduled by cron every night at a given time.
Any ideas?

TIA

Hmm

~/.cache$ du -h -d 1 | sort -h
1.8M    ./clangd
1.8M    ./mesa_shader_cache
3.4M    ./fontconfig
9.2M    ./esbuild
101M    ./go-build
624M    ./arduino
751M    .

over half a gig.

~/.cache$ du -h -d 1 arduino/ | sort -h
73M     arduino/cores
551M    arduino/sketches
624M    arduino/

You should probably keep the cores until you update a board or boards plural. Then wipe that and let them re-cache on the next build(s).

Looks like the sketches are also kept forever. Shouldn't be a problem wiping that nightly, as long as that's not when you're actually in the middle of building something.

But wait... let me check another computer (a Mac)

$ du -h -d 1 arduino/ | sort -h
267M	arduino/cores
6.9G	arduino/sketches
7.2G	arduino/

OK, that's way too much. Check a couple settings

$ ./arduino-cli config get build_cache
compilations_before_purge: 10
path: /Users/kenb4/Library/Caches/arduino
ttl: 720h0m0s

So every ten compiles, it should purge everything older than 30 days. That ain't happening.

OK, here's the main problem. During a build, the temp directory for that specific sketch is computed

and then defer maybePurgeBuildCache is called, but only with the purge count and TTL from the settings. But at the end of that function

if the purge count passes, the call is made with the TTL. But that's the wrong root directory -- it's under /temp or /private -- when instead it should be under ~/.cache or Caches. So nothing ever gets purged.

The minimum fix is to pass the recently-computed buildCachePath as an additional argument and then

-       buildcache.New(paths.TempDir().Join("arduino", "cores")).Purge(cacheTTL)
-       buildcache.New(paths.TempDir().Join("arduino", "sketches")).Purge(cacheTTL)
+       buildcache.New(buildCachePath.Join("cores")).Purge(cacheTTL)
+       buildcache.New(buildCachePath.Join("sketches")).Purge(cacheTTL)

Probably, but why? I have never done anything like that.

Hi @domify80. When you compile a sketch in Arduino IDE, it first checks this ~/.cache/Arduino/sketches folder to see if it has already cached the compiled output for the given Arduino boards core, library, or sketch file. If so, it simply reuses the cache instead of compiling from source. This reduces the duration of the compilation operation. That can save a lot of time in the case of complex cores, libraries, or sketches.

You can delete that folder without breaking anything, but if you do that then Arduino IDE will have to compile everything from source once again.

If you are really short on hard drive space, then maybe it would make sense to delete this folder, but otherwise you are only hurting yourself by doing that. Even if you are short on hard drive space, I would recommend looking elsewhere to see if you can remove something that serves no purpose.

After fixing the CLI temp-vs-cache directory bug -- and ten builds to trigger a purge -- 7.2G disk used went down to

$ du -h -d 1 arduino/ | sort -h
 37M	arduino/cores
938M	arduino/sketches
975M	arduino/

For whatever reason, a lot of the directories are missing the .last-used file checked for the TTL. In that case

that Stat() error is never logged, and the function just returns. Fixing that, to just remove such directories, disk usage is now down to

$ du -h -d 1 arduino/ | sort -h
 37M	arduino/cores
125M	arduino/sketches
161M	arduino/

Much more reasonable.

Thanks for bringing this bug to our attention! I submitted a formal report to the Arduino CLI developers on your behalf:

Thanks! Your write-up is a lot better than mine would have been.