The short answer
- 01
Safe to delete. ~/go/pkg/mod holds downloaded module files shared by every Go project on your Mac.
- 02
The go command writes it on go build, go test, go get and go mod download, and never removes anything by itself.
- 03
You lose download time: the next build downloads each module again.
- 04
The command: go clean -modcache. Plain rm -rf fails because Go makes the files read-only.
See its real size on your Mac. Hoardless measures this folder and previews the delete.Download free trial
What is the ~/go/pkg/mod folder?
It is Go's module cache. The Go modules reference calls it the directory where the go command stores downloaded module files, and gives its default location as $GOPATH/pkg/mod. GOPATH defaults to a folder named go in your home, so on a Mac the cache is ~/go/pkg/mod.
go env GOMODCACHE
Prints the active location. GOMODCACHE overrides the default. Removes nothing.
The module cache is separate from the build cache, which holds compiled packages in ~/Library/Caches/go-build (the go-build folder in your user cache directory).
What writes it and why does it grow?
Any go command that needs a module downloads it here: go build, go test, go get and go mod download. Every version stays, for every project.
- No limit and no pruning: Go's docs say the module cache has no maximum size and the go command does not remove its contents automatically.
- Read-only files: Go creates module files and folders read-only, to prevent accidental changes after download.
- Download metadata too: the
cache/downloadfolder inside keeps the zip files and checksums the sources came from.
How big does the Go module cache get?
It depends on how many modules and versions your projects use, so there is no normal size. Measure your own:
du -sh "$(go env GOMODCACHE)"
Prints the module cache size. Removes nothing.
du -sh "$(go env GOCACHE)"
Prints the build cache size, for comparison. Removes nothing.
Is it safe to delete the Go module cache?
Yes: it only holds downloaded modules, and the go command fetches them again when a build needs them.
| What | Verdict | What you lose |
|---|---|---|
~/go/pkg/mod, via go clean -modcache | Safe | Downloaded modules. The next build downloads them again. |
~/Library/Caches/go-build | Safe | Compiled packages. The next build is slower. |
| Deleting before working offline | Careful | Builds fail without a network until the modules are downloaded again. |
~/go/bin and your code under ~/go/src | Never | Tools you installed with go install, and any projects you keep in GOPATH. |
How do I delete the Go module cache?
Use Go's own command:
go clean -modcache
Removes the entire module cache, including unpacked sources. Go's docs say this is usually the best way to remove it, because rm -r fails on the read-only folders.
go clean -cache
Optional: removes the whole build cache too. Go also deletes build cache entries it has not used recently on its own.
The manual way: if you build with -modcacherw (for example through go env -w GOFLAGS=-modcacherw), new cache folders are writable and rm -r works. Go's docs warn this raises the risk of editors and tests changing files in the cache. Without it, a Finder drag to the Trash may stop on permissions.
What Hoardless does: its Cargo and Go detector lists ~/go/pkg/mod at the Auto tier and runs go clean -modcache, using the go your login shell finds. It never trashes the folder by path. When go is not installed, the item stays as that command and reports that go is missing when run.
What comes back and when?
Only the modules your next builds need. The first go build or go test in each project downloads the versions its go.mod lists. Versions nothing uses any more stay gone.
Because Go never trims the module cache, clearing it now and then is the only way to drop versions you no longer use.
Clear Go modules the way Go expects
Hoardless finds the Go module cache (Auto tier) and runs go clean -modcache instead of fighting read-only files, next to Cargo, Homebrew, Gradle and Maven caches. You review the plan first.
14-day full trial. macOS 14 or later.
Questions
Why does rm -rf ~/go/pkg/mod say permission denied?
Go creates module files and folders read-only so nothing changes them after download. go clean -modcache removes them anyway, which is why Go's docs recommend it.
What is GOMODCACHE?
The environment variable that sets the module cache location. When it is not set, the cache is $GOPATH/pkg/mod, and GOPATH defaults to ~/go.
Is ~/Library/Caches/go-build the same cache?
No. That is Go's build cache of compiled packages. go clean -cache clears it, and Go also deletes entries it has not used recently on its own.
Can I delete only old module versions?
Go has no command for that. go clean -modcache removes the whole module cache; modules your projects still need download again.
New guides by email
New guides and Hoardless releases, twice a month at most.
Confirm first. Unsubscribe in one click. Privacy.