What is go clean command

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.

Syntax

go clean [flags] [packages]
  1. flags: Optional flags to modify the behavior of the command.
  2. 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’

  1. -i: Remove the corresponding installed archive or binary (what ‘go install’ would create).
  2. -r: Clean recursively, i.e., apply the clean command to all dependencies.
  3. -cache: Clean the entire module cache (usually located in $GOPATH/pkg/mod), including both cached packages, builds, and cached downloaded modules.
  4. -testcache: Clean the entire test result cache.
  5. -modcache: Clean the entire module download cache.

Example

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.