Clean Go Build Outputs Without Deleting the Wrong Cache

go clean can wipe more than you meant: past the project's own build artefacts sit shared caches that every other Go project on the machine relies on. This guide previews before it deletes, separates a package clean from a cache clean, and checks the project still works afterwards. Allow about ten minutes for a routine project clean, plus time to redownload dependencies if you clear a cache.

This guide describes the installed Go command on this machine, Go 1.26.1 from the golang-go:amd64 package. The Debian manual page is dated 15 March 2022, while the installed command confirms the cache flags shown here. A different Go release can have different flags or cache details, so check its local help first.

1. Check the command and your project

Start in the module or package tree you intend to clean. Do not run a broad clean from an unrelated directory just because the command happens to work there. Confirm the executable and version:

$ command -v go
/usr/bin/go
$ go version
go version go1.26.1 linux/amd64

There is normally no need for sudo. A project clean should be unprivileged. If files are owned by another account, stop and fix the ownership or switch to the correct account rather than making a habit of running Go as root.

Checkpoint: you are in the intended project directory, and go version reports the toolchain you expect.

2. Preview an ordinary project clean

Use -n to print the removal commands without executing them. This is the safest way to see what the installed tool considers removable for the selected package:

$ go clean -n

The preview can be empty. Modern Go puts most build results in a temporary directory or the shared build cache, so there may be no old object files left in the source tree. This command is mainly useful for files left by older tools, manual builds, tests compiled with go test -c, or similar workflows.

For a named package, pass its import path or a package pattern after the flags:

$ go clean -n ./...
$ go clean -n example.com/project/cmd/server

Read the complete preview before running the same command without -n. The output is a list of removal commands, not a report of files already deleted.

3. Remove package-local artefacts

Once the preview looks right, run the corresponding clean:

$ go clean ./...

With a package argument, Go examines the matching source directories. The installed manual lists old _obj/ and _test/ directories, generated _testmain.go, test.out, build.out, object files such as *.o, binaries produced by go build, test binaries, and shared objects produced by SWIG. The exact set depends on the package tree and the files actually present.

The -r flag extends the clean to dependencies of the named packages. Use it only when that wider scope is intentional:

$ go clean -n -r ./...
$ go clean -r ./...

The -i flag also removes the corresponding installed archive or binary that go install would create. Treat -i as a separate decision, especially on a development machine where another project may rely on an installed command:

$ go clean -n -i ./...
$ go clean -i ./...

Warning: these removals are destructive. Source files are not a backup of generated binaries or test output. If you might need an artefact for debugging, copy it to a named evidence directory before cleaning. If you delete it by accident, rerun the relevant build or test command to recreate it; there is no undo command in go clean.

4. Choose the cache you actually mean

Cache flags affect shared data outside the current package directory. Do not reach for them as a reflex when a local clean would do.

Preview each cache operation before running it:

$ go clean -n -cache
$ go clean -n -testcache
$ go clean -n -modcache
$ go clean -n -fuzzcache

Then run only the operation you actually selected, for example:

$ go clean -cache

Cache flags do not need a package argument; they target the Go cache configured for the current user and environment. Check the locations before a large cleanup:

$ go env GOCACHE GOMODCACHE GOPATH

Do not combine several cache flags in a script unless the wider reset is deliberate and documented. A clean cache is recoverable by rebuilding or redownloading, but it can cost time, bandwidth and access to private modules.

5. Verify the result

go clean is quiet on success, so verify with the operation that matters. Rebuild a package to repopulate normal build outputs:

$ go build ./...
$ go test ./...

If the project has a dedicated test or build command, use that instead. A successful clean does not prove the project still builds, and a successful build does not prove a module cache purge was harmless to an offline workflow. For a local artefact clean, inspect the working tree afterwards:

$ git status --short

Unexpected source changes are not a normal result of go clean. Stop and investigate rather than restoring files blindly; the command should not touch tracked Go source, module files or test data.

Common traps

Done means