Make sure to clean your Go build cache
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!
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
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
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
The trim.txt file stores a Unix timestamp. I'm guessing it's the timestamp of the time you run the clean command.
In addition to the build cache Go also has a few other caches.
-testcacheexpires all the test results cache inside the build cache
-fuzzcacheremove files stored in the Go buildcache for fuzz testing.
Both of those flags clean caches that are inside the build cache directory.
-modcacheremove the entire module download cache located at
All of this information is available with
go help clean