Skip to content

How to Clear or Disable Go Build Cache

DodaTech 2 min read

In this tutorial, you'll learn about How to Clear or Disable Go Build Cache. We cover key concepts, practical examples, and best practices.

The Problem

Your Go binary behaves differently than expected, or a previously passing test now fails because Go's build cache is serving a stale compiled artifact. The build cache lives at the path returned by go env GOCACHE (typically ~/.cache/go-build) and persists across sessions. Switching branches, upgrading Go versions, or changing build tags can leave stale cache entries that mask real compilation or runtime issues.

Quick Fix

1. Clear the entire build cache

go clean -cache

Expected output:

# (no output — cache cleared silently)

Verify the cache is empty:

go env GOCACHE
du -sh $(go env GOCACHE)

Expected output:

/home/user/.cache/go-build
32K     /home/user/.cache/go-build

2. Clear only test results

go clean -testcache

This removes cached test results while keeping compiled package objects. Use this when you changed test code and want to re-run tests without recompiling all dependencies.

3. Clear the module download cache

go clean -modcache

Expected output:

# (no output — module source removed)

This deletes all downloaded module source from $GOPATH/pkg/mod/. The next build will re-download everything.

4. Check current cache location and size

go env GOCACHE GOMODCACHE
du -sh $(go env GOCACHE) $(go env GOMODCACHE)

Expected output:

/home/user/.cache/go-build
/home/user/go/pkg/mod
2.3G    /home/user/.cache/go-build
850M    /home/user/go/pkg/mod

5. Disable the cache for a single build (debugging only)

GOCACHE=off go build -a -v ./...

The -a flag forces recompilation of all packages, and GOCACHE=off prevents writing new cache entries. This is useful for isolating cache-related bugs but slows down builds significantly.

6. Force a complete clean rebuild

go clean -cache -testcache -modcache
go build -v ./...

Expected output:

github.com/example/app/internal/config
github.com/example/app/internal/db
github.com/example/app/cmd/server

7. Check the Go version's cache directory structure

ls -la $(go env GOCACHE)

Expected output:

drwxr-xr-x 2 user user 4096 Jan 15 10:30 .
drwxr-xr-x 2 user user 4096 Jan 15 10:30 ..
-rw-r--r-- 1 user user  123 Jan 15 10:30 log.txt

The cache directory contains subdirectories named after the hash of each build artifact.

7. Use go clean to remove object files without clearing the full cache

go clean -cache -testcache -modcache

This removes cached test results and module downloads but keeps compiled packages.

8. Disable cache for a single build

go build -a ./...

The -a flag forces all packages to be rebuilt regardless of cache state. Use this when you suspect a cache issue but want to avoid a full clear.

Prevention

  • Keep GOCACHE on a fast SSD — it improves incremental build speed significantly
  • Set -count=1 on go test to disable test result caching for individual test runs
  • Clear the cache only when you suspect stale artifacts, not as a routine maintenance step

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro