Is it safe to delete ~/go/pkg/mod and Go’s build cache?
- Yes, with Go’s own commands. The module cache holds downloaded dependencies and the build cache holds compiled packages, and both come back. Use
go clean, because module files are read-only andrm -rffails on them. - What it costs you: the next build downloads modules again and compiles from scratch.
- Does it come back? Yes. The build cache trims itself; the module cache only grows until you clean it.
- SiftMac’s verdict: Rebuilds
1. Two caches
- The module cache (
GOMODCACHE) holds the source of every module version your projects downloaded, plus the original zip andgo.modfiles undercache/download. Go’s reference says it has no maximum size and thegocommand doesn’t remove its contents automatically. - The build cache (
GOCACHE) holds compiled packages and test results, so unchanged code isn’t compiled twice. Thegocommand periodically deletes data in it that hasn’t been used recently.
Go creates module files and folders read-only so tests and editors can’t change a dependency after it was checked against go.sum. That protection is also why deleting the folder by hand fails with permission errors.
2. Where they are
~/go/pkg/mod
~/Library/Caches/go-build
The module cache is pkg/mod inside GOPATH, which defaults to ~/go. The build cache is a go-build folder in your Mac’s user cache folder. Print both:
go env GOMODCACHE GOCACHE
3. How big they get
du -sh "$(go env GOMODCACHE)" "$(go env GOCACHE)"
Since nothing trims it, the module cache is the one that keeps growing: every version of every module, across all your projects and every upgrade.
4. How to clear them
go clean -modcache
go clean -cache
go clean -modcacheremoves the entire module cache, unpacked source included. Go’s reference calls it usually the best way, because it deals with the read-only permissions for you.go clean -cacheremoves the entire build cache.go clean -testcacheonly expires cached test results, if stale passes are the problem.- Leave
go clean -fuzzcachealone unless you mean it: it deletes inputs the fuzzer found that expand coverage, which makes fuzzing less effective.
If you’d rather rm -rf worked in future, go env -w GOFLAGS=-modcacherw makes new module folders writable. Go’s docs ask for caution with it, since writable modules can be changed by accident; go mod verify detects such changes.
5. What happens afterwards
- The next
go build,go testor editor load downloads the modules ingo.modagain through your module proxy and checks them againstgo.sum. - The first build compiles everything; later builds are quick again as the build cache refills.
- Projects that vendor their dependencies in a
vendorfolder build without downloading. - Programs you installed with
go installkeep working. They live in~/go/bin, whichgo clean -modcachedoesn’t touch.
Questions
Why does rm -rf ~/go/pkg/mod say Permission denied?
Go creates module files and folders read-only to protect them from accidental edits, and rm can’t remove files from a folder it can’t write to. go clean -modcache handles that for you.
Is it safe to delete ~/go entirely?
Usually not. Besides the cache, ~/go/bin holds tools you installed with go install, and older projects may keep code in ~/go/src. Clear pkg/mod with go clean -modcache instead.
Does Go clean these caches by itself?
The build cache, yes: Go periodically deletes entries that haven’t been used recently. The module cache, no: it has no size limit and stays until you clean it.
Can several Go projects share the module cache safely?
Yes. Every project on the Mac uses the same module cache, and Go’s reference says several go commands can safely use it at the same time.
How SiftMac handles it. Its App caches category clears Go’s build cache in ~/Library/Caches/go-build as a download cache with a Rebuilds verdict. It leaves the module cache in ~/go/pkg/mod to go clean -modcache, which handles its read-only files; the space map shows how big it is.
Download SiftMac · scanning is free, and so is your first 2 GB of cleaning · See how it works