What is ~/go/pkg/mod, and is it safe to delete?

The ~/go/pkg/mod folder is Go's module cache of downloaded dependencies, and it is safe to clear with go clean -modcache because Go downloads them again.

Shiva ChandraMaker of Clean House · · 2 min read
Short answer

~/go/pkg/mod is Go's module cache, where the go command keeps downloaded dependencies for all your projects. It is safe to clear. Run go clean -modcache, because the files are read-only and rm may complain. Go downloads the modules again the next time a build needs them.

Path
~/go/pkg/mod
What it is
Downloaded Go modules, stored read-only
Typical size
Hundreds of megabytes to several gigabytes across many projects
Made by
The go command
Safe to delete?
Safe to clear
Comes back?
Yes, go build or go mod download fetches modules again

What is in this folder?

By default the module cache is at $GOPATH/pkg/mod, and $GOPATH defaults to ~/go. The Go Modules Reference says Go creates read-only files there to prevent accidental changes. Each version of each module has its own folder, so older versions pile up. Run go env GOMODCACHE to see the path.

Your own Go source code lives in your project folders, not here.

Is it safe to delete?

Yes. The cache is rebuilt from the module proxy, and go.sum in each project verifies the downloads. The cost is download time, and you need network access for the next build.

The sibling folder ~/go/bin holds programs you installed with go install. Leave it alone unless you want to remove them.

How do I clear it by hand?

  1. Stop any running go build or editor language server (gopls).
  2. Run the Go command, which handles the read-only files.
  3. Build a project to download what it needs.
go env GOMODCACHE
go clean -modcache
Prefer go clean -modcache to rm -rf, since the cache files are read-only. Run go clean -cache separately for the build cache.

How do I stop it growing?

Run go clean -modcache occasionally. Go does not trim the module cache itself.

How Clean House handles it

Clean House recognizes ~/go/pkg/mod as a Go modules cache made by Go and marks it as downloading again. The Why card spells that out before you clear it, and files go to the Trash first, with Recently Cleared as an undo.

Questions

Is it safe to delete ~/go/pkg/mod?

Yes. Go downloads modules again on the next build. You need a network connection.

How do I clear the Go module cache?

Run go clean -modcache.

Where is the Go module cache?

At $GOPATH/pkg/mod, which is ~/go/pkg/mod by default. go env GOMODCACHE prints it.

Sources

  1. Go: Go Modules Reference
  2. Go: go command documentation