go clean: Usage, Options & Common CI Errors
Remove build outputs and caches.
go clean deletes object files from package source directories and, with flags, clears the shared build, test, and module caches. The cache flags are the ones that matter in CI.
What it does
By default removes per-package build artifacts. -cache clears the build cache, -testcache expires cached test results, and -modcache deletes the downloaded module cache entirely.
Common usage
go clean # remove package build artifacts
go clean -cache # clear the build cache
go clean -testcache # invalidate cached test results
go clean -modcache # wipe the module cacheCommon CI error: wiping the cache slows builds
Adding go clean -modcache to "fix" a flaky build deletes the module cache you restore each run, so every CI run re-downloads everything and gets slower. Fix: only clear caches to recover from a genuine corruption (e.g. after a checksum mismatch); otherwise leave them cached. For a stuck test result, prefer go test -count=1 over -testcache.
Options
| Flag | Effect |
|---|---|
| -cache | Clear the build cache |
| -testcache | Expire cached test results |
| -modcache | Remove the module cache |
| -i | Also remove installed archives/binaries |