Useless Anecdote

I realized that my storage was quickly getting filled out. I had recently added a new user to my system so I thought maybe that accounted for the sudden spike in storage usage. When I finally checked what was actually going on, I saw that my Go build cache directory was taking up about 35GB!

Contents of ~/.cache

Locate Go build cache directory

The default location for build cache data is a directory named go-build in the standard cache directory. For me, on Linux, it is ~/.cache/go-build/. You can find out yours using this command

go env GOCACHE

Go also conveniently allows you to modify the cache location using the environment variable GOCACHE

Check disk usage of Go build cache

Here's a quick one-liner to see the disk usage of the build cache

du -hs $(go env GOCACHE)

Content of Go build cache

Contents of ~/.cache/go-build/ Contents of go-build cache using Diskonaut

Finally, to address the question of this article, you can run this command

go clean -cache

This command removes all the subdirectories inside go-build directory and leaves out just two files

  • README
  • trim.txt

The trim.txt file stores a Unix timestamp. I'm guessing it's the timestamp of the time you run the clean command.

Other caches

In addition to the build cache Go also has a few other caches.

  • -testcache expires all the test results cache inside the build cache

  • -fuzzcache remove files stored in the Go buildcache for fuzz testing.

Both of those flags clean caches that are inside the build cache directory.

  • -modcache remove the entire module download cache located at $GOPATH/pkg/

All of this information is available with go help clean

References