go-clean
Remove object files and cached data
TLDR
Print the remove commands instead of actually removing anything
Delete the build cache
Delete all cached test results
Delete the module cache
SYNOPSIS
go clean [flags] [packages]
Removes object files and cached build artifacts from Go workspaces.
PARAMETERS
-i
Removes the corresponding installed archive or binary created by go install.
-n
Prints the commands that go clean would execute, but does not run them.
-x
Prints the commands that go clean executes as it runs.
-cache
Removes the entire Go build cache, located at GOCACHE. This can significantly free up disk space but may slow down subsequent builds as everything needs to be recompiled.
-testcache
Expires all test results in the Go build cache. Subsequent go test commands will re-run tests.
-modcache
Removes the entire Go module download cache, located at GOMODCACHE. This forces modules to be re-downloaded upon demand.
-fuzzcache
Removes the entire Go fuzzing cache, located at GOFUZZCACHE. (Introduced in Go 1.18).
DESCRIPTION
go clean is a crucial command within the Go programming language toolchain, designed to remove object files, executable binaries, and cached test results from a Go workspace. Its primary purpose is to help developers manage disk space, ensure fresh builds by clearing potentially stale or corrupted artifacts, and troubleshoot build issues. By default, it operates on the current module or specified packages, deleting files created by go build, go install, and go test -c. The command can also target the global Go build cache (GOCACHE), module download cache (GOMODCACHE), and fuzzing cache (GOFUZZCACHE), providing a comprehensive way to reset the build environment. This flexibility makes go clean an essential utility for maintaining a clean and efficient Go development workflow.
CAVEATS
- go clean only removes files specifically created by the Go toolchain (go build, go install, go test -c). It does not clean up arbitrary project output directories or files not managed by Go.
- Using flags like -cache or -modcache can delete a substantial amount of data. While this frees disk space, it will cause subsequent builds and module resolutions to be slower as they must re-download or rebuild everything.
- The command operates on the current working directory and its subdirectories by default, or on specified packages. Be mindful of the context when executing go clean.
ENVIRONMENT VARIABLES
The behavior of go clean is influenced by several environment variables that define the locations of caches it manages:
GOCACHE: Specifies the location of the Go build cache.
GOMODCACHE: Specifies the location of the Go module download cache.
GOFUZZCACHE: Specifies the location of the Go fuzzing cache.
USAGE SCENARIOS
go clean is typically used for:
1. Freeing up disk space by removing compiled binaries and caches.
2. Ensuring a fresh build by clearing stale artifacts before recompilation.
3. Troubleshooting build or dependency resolution issues that might be caused by corrupted cache entries.
HISTORY
go clean has been an integral part of the Go programming language toolchain since its inception, serving as a fundamental utility for managing build artifacts. As Go evolved, particularly with the introduction of the build cache in Go 1.8 and modules in Go 1.11, the command gained new functionalities to manage these additions. Flags like -cache and -modcache were introduced to specifically clear these new caching mechanisms, adapting go clean to the changing landscape of Go's dependency management and build optimization. The -fuzzcache flag was added in Go 1.18 to support the new fuzzing capabilities. Its core function of removing object files remains consistent, evolving to address new challenges in the Go ecosystem.