How to clear Go cache on Mac: go clean -cache and -modcache

It’s safe to clear Go cache on a Mac: Go keeps two caches, one of compiled packages and test results and one of downloaded modules, and builds or downloads again what a project needs, while your code stays. Go trims old build results itself but never removes downloaded modules, so this guide shows how to clear each cache and why deleting the module folder by hand fails.

MacWashing, System Junk: Xcode junk, user caches, installers and logs found, 22.83 GB ready to be cleaned
  1. Open MacWashing and choose System Junk in the sidebar.
  2. Click Start Scan.
  3. Look through what it found and click Clean. Anything debatable stays unchecked.

Free trial · macOS 14 or later

Where Go keeps its caches

Go has two caches. The build cache holds compiled packages and test results. The module cache holds the source code of the dependencies Go downloaded. This command shows the build cache folder, by default ~/Library/Caches/go-build:

go env GOCACHE

And this one shows the module cache folder, by default ~/go/pkg/mod:

go env GOMODCACHE
Terminal: the folders of Go’s build cache and module cache

How to clear the Go build cache

This command deletes everything in the build cache: compiled packages and cached test results. Your code and downloaded modules stay. The next build compiles everything again:

go clean -cache

To clear only cached test results, so go test runs every test again instead of printing (cached), use go clean -testcache. go clean -fuzzcache removes the files fuzz tests keep in the build cache.

Go deletes build results it hasn’t used recently by itself, so the build cache rarely grows without limit. Clear it when you need the space now.

How to clear the module cache with go clean -modcache

This command deletes every downloaded module, including its unpacked source code. Your projects, go.mod and go.sum stay. The next build downloads the modules it needs again, so it needs the internet:

go clean -modcache

Go never removes anything from the module cache by itself, and the cache has no size limit. On a Mac used for Go for a few years, it is usually the bigger of the two.

Why rm -rf fails on ~/go/pkg/mod

Go makes the module files and folders read-only, so editors and tests can’t change a dependency by accident. That is why rm -rf ~/go/pkg/mod ends with “Permission denied”. Use go clean -modcache instead: it deletes the cache whatever the permissions.

With MacWashing, it’s one click

MacWashing, System Junk: Xcode junk, user caches, installers and logs found, 22.83 GB ready to be cleaned

System Junk in MacWashing lists the Go build cache and the module cache under App Caches, both unchecked: Go would have to compile or download everything again, so you decide. The caches of your other apps are cleared in the same pass.

Free trial · macOS 14 or later

Questions and answers

Will MacWashing delete my own files when it cleans?

No. It removes only what it can match to a known app or a known kind of junk, in places apps leave files; macOS and Apple’s files are never touched. Anything debatable is marked and stays unchecked until you decide.

What about apps that are open?

If an open app holds files being cleaned, MacWashing waits for it to quit or quits it at your request, and opens it again afterwards.

Is it free?

Yes, within the free trial: System Junk and the Uninstaller free up to 25 GB in total, with no account or card needed.

Is it safe to clear Go cache?

Yes. Both caches hold things Go can build or download again. The first build afterwards is slower, and after go clean -modcache it needs the internet.

What is the difference between go clean -cache and go clean -modcache?

-cache clears compiled packages and test results. -modcache clears the source code of downloaded dependencies. They are separate folders, and each command clears only its own.

Why does the Go cache grow back?

Every build adds compiled packages, and every new dependency version is downloaded into the module cache. Go trims old build results itself but keeps every module version until you clear the module cache.