The go clean command removes object files and cached files created during the build process. It can help you clean up your project directory and free up disk space by deleting compiled binary files, test binaries, and other temporary files.
The go clean command does not remove dependencies or packages installed with go get.
Syntax
go clean [flags] [packages]
- flags: Optional flags to modify the behavior of the command.
- packages: The list of packages to clean. If no packages are specified, the command defaults to the current package (the package in the current directory).
Useful flags of ‘go clean command’
- -i: Remove the corresponding installed archive or binary (what ‘go install’ would create).
- -r: Clean recursively, i.e., apply the clean command to all dependencies.
- -cache: Clean the entire module cache (usually located in $GOPATH/pkg/mod), including both cached packages, builds, and cached downloaded modules.
- -testcache: Clean the entire test result cache.
- -modcache: Clean the entire module download cache.
Example
To clean the current package and remove the corresponding installed binary in Golang, you can use this command: go clean -i.
Please note that go clean does not uninstall packages or dependencies installed with go get.
To remove those, you’ll need to manually delete their directories, as described in the previous answer.
That’s it.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Distributed and cloud computing and is an expert in Go Language.