Skip to content
Latchkey

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

Terminal
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 cache

Common 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

FlagEffect
-cacheClear the build cache
-testcacheExpire cached test results
-modcacheRemove the module cache
-iAlso remove installed archives/binaries

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →