The go clean command is “used to remove 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.
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.
To clean the current package and remove the corresponding installed binary, 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.