Removing a Go package depends on how the package was installed.
A package may exist as a module dependency in go.mod, a globally installed CLI tool, or a cached module downloaded by Go.
The quick reference below shows the most common commands used to remove Go packages in different situations.
Quick Cheat Sheet: Remove or Uninstall a Go Package
| Task | Command |
|---|---|
| Remove Go module dependency | go get module@none |
| Clean unused dependencies | go mod tidy |
| Check why a dependency exists | go mod why module |
| View module dependency graph | go mod graph |
| Remove globally installed Go tool | rm $(go env GOPATH)/bin/tool-name |
| Clear Go module cache | go clean -modcache |
These commands should be executed from the root directory of your Go project, where the
go.modfile is located.
Scenario: Remove a Dependency from Your Go Project
If a Go package was added as a dependency in your project, it must be removed from the module dependency graph instead of manually editing files. The following steps ensure the module is removed safely without breaking your build.
Step 1: Confirm the module exists in your project
First verify that the dependency exists in your project's module list.
go list -m all | grep ginExample output:
github.com/gin-gonic/gin v1.9.1This confirms that the module is currently included in your project's dependency graph.
Step 2: Check whether your code imports the package
Before removing the dependency, confirm that your application code does not directly import it.
You can search the project for imports:
grep -r "github.com/gin-gonic/gin" .If the package appears in your code, remove the import statement first and update the code accordingly.
Step 3: Verify dependency usage using go mod why
The go mod why command explains why a module is required.
go mod why github.com/gin-gonic/ginExample output:
# github.com/gin-gonic/gin
example.com/myproject
github.com/gin-gonic/ginIf another module requires it, the dependency cannot be removed until the parent dependency is removed.
Step 4: Remove the module using go get module@none
Once you confirm the dependency is not required, remove it from the module graph.
go get github.com/gin-gonic/gin@noneThis tells Go to drop the dependency from go.mod.
Step 5: Clean unused dependencies using go mod tidy
Finally, clean the dependency list.
go mod tidyThis command:
- removes unused dependencies
- updates the
go.modfile - updates the
go.sumfile
Your project will now contain only the dependencies required by your code.
Scenario: Undo a Package Added with go get
Sometimes a dependency may be added accidentally using go get. In Go Modules, undoing this simply means removing the dependency from the project's module requirements.
Understand how go get modifies go.mod
When you run a command like:
go get github.com/gin-gonic/ginGo performs two actions:
- Adds the module requirement to
go.mod - Downloads the module into the local module cache
If the dependency is unused, it can be removed safely.
Remove the dependency safely
To remove the dependency, instruct Go to remove the module requirement.
go get github.com/gin-gonic/gin@noneThis removes the module entry from the project's dependency graph.
Run go mod tidy to update dependencies
After removing the module, run:
go mod tidyThis cleans unused dependencies and updates both go.mod and go.sum.
Verify the dependency was removed from the project
To confirm the module was removed, list the project's dependencies:
go list -m all | grep ginIf no output appears, the dependency has been successfully removed from the project.
Scenario: Uninstall a Globally Installed Go Tool
Sometimes what users call a “Go package” is actually a command-line tool installed with go install. These tools are compiled binaries stored in your Go binary directory, not dependencies inside go.mod.
Understand how Go installs CLI tools
When you run a command like:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latestGo compiles the program and places the binary in your Go binary directory.
This directory is either:
$GOBIN(if explicitly configured)$GOPATH/bin(default location)
These tools are independent of your project dependencies.
Locate installed binaries using GOBIN or GOPATH
Check where Go installs binaries:
go env GOBIN
go env GOPATHIf GOBIN is empty, binaries are stored in:
$GOPATH/binExample:
/home/user/go/binRemove the tool binary from $GOPATH/bin
To uninstall the tool, simply delete the binary.
Example:
rm -f $(go env GOPATH)/bin/golangci-lintThis removes the executable from your system.
Verify the tool is removed from your system
You can confirm the tool is gone using:
which golangci-lintIf no path is returned, the tool has been successfully removed.
Troubleshooting: Why Does the Package Keep Coming Back?
Sometimes a dependency appears again after removal. This happens because Go automatically rebuilds the dependency graph based on required imports.
Detect indirect dependencies in Go modules
Some modules are included because another dependency requires them.
In go.mod they may appear like this:
github.com/example/module v1.2.3 // indirectThis indicates the module is required by another package.
Inspect dependency chains using go mod why
Use the following command to understand why a dependency exists:
go mod why github.com/example/moduleExample output:
# github.com/example/module
example.com/project
github.com/some-library
github.com/example/moduleThis shows which module in your project requires it.
Understand how Go rebuilds the dependency graph
Whenever you run:
go mod tidyGo analyzes your imports and rebuilds the dependency graph. If another package still depends on the module, Go will automatically re-add it.
This ensures your project always has the dependencies required to build successfully.
Decide whether removing the dependency is possible
If a module is required by another dependency, you cannot remove it directly.
Your options are:
- remove the parent dependency
- replace it with another package
- keep the dependency as required by the project
Bonus: Clean the Local Go Module Cache
Even after removing a dependency from your project, Go may keep the downloaded package in the local module cache.
Understand what the Go module cache stores
Go downloads modules into a local cache directory so they can be reused by multiple projects.
This cache is usually located under:
$GOPATH/pkg/modRemoving a dependency from a project does not delete it from the cache.
Remove cached modules from your system
To clear all cached modules, run:
go clean -modcacheThis removes every downloaded module from the cache.
Free disk space used by downloaded packages
Large Go projects can download hundreds of dependencies. Clearing the module cache can free significant disk space.
After clearing the cache, Go will automatically download modules again when required.
Force Go to re-download dependencies
After clearing the cache, the next build will re-download dependencies:
go mod downloador simply run:
go buildGo will fetch the required modules again.
Common Mistakes When Removing Go Packages
Removing Go dependencies incorrectly can lead to confusing behavior. The following mistakes are common among developers.
Running commands outside the Go project directory
Commands such as go mod tidy only work inside a directory that contains go.mod.
Verify your location using:
go env GOMODIf the output shows /dev/null, you are not inside a Go module.
Deleting dependencies manually from go.mod
Manually removing lines from go.mod is unreliable.
If another dependency still requires the module, Go will restore it automatically when you run:
go mod tidyAlways remove dependencies using:
go get module@noneConfusing Go tools with module dependencies
Some users attempt to remove CLI tools using module commands.
Remember:
| Item | Removal Method |
|---|---|
| Project dependency | go get module@none |
| Installed CLI tool | delete binary from $GOPATH/bin |
| Cached module | go clean -modcache |
Clearing cache instead of removing dependencies
Running:
go clean -modcachedoes not remove dependencies from your project.
It only deletes downloaded modules from your system cache. The dependency will still appear in go.mod if your project requires it.
Frequently Asked Questions
1. How do I uninstall a Go package installed with go get?
To uninstall a Go package dependency, run 'go get module@none' and then execute 'go mod tidy'. This removes the dependency from go.mod if it is no longer required by the project.2. Does Go have an uninstall command for packages?
Go does not provide a single uninstall command. Instead, packages are removed by deleting the module dependency from go.mod, removing installed binaries, or clearing the module cache.3. How do I undo go get in Go?
You can undo a dependency added with go get by running 'go get module@none' followed by 'go mod tidy'. This removes the dependency if it is not used by your code.4. How do I remove a globally installed Go tool?
Go tools installed with 'go install' are stored in the GOPATH or GOBIN directory. You can uninstall them by deleting the corresponding binary from '$GOPATH/bin'.5. Why does a Go package reappear after removing it?
A Go package may reappear after removal if another dependency still requires it. Running 'go mod tidy' will restore required dependencies automatically.Summary
Removing a Go package depends on how the package was added to your system.
In modern Go projects using Go Modules, dependencies should be removed by updating the module graph rather than manually editing files.
The most common approaches are:
- Remove a project dependency using
go get module@none - Clean unused modules with
go mod tidy - Uninstall CLI tools by deleting binaries from
$GOBINor$GOPATH/bin - Clear downloaded modules using
go clean -modcache
Understanding whether the package is a module dependency, a globally installed tool, or a cached module helps ensure you remove it correctly without breaking your project.
Official Documentation
You can learn more about Go module management and dependency handling from the official Go documentation.





![GO Bytes to String Conversion Best Practices [5 Methods]](/go-bytes-to-string/golang-byte-to-string_hu_e997372962029da.webp)




