How to Remove or Uninstall a Go Package (go get, Go Modules, Tools)

How to Remove or Uninstall a Go Package (go get, Go Modules, Tools)

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

TaskCommand
Remove Go module dependencygo get module@none
Clean unused dependenciesgo mod tidy
Check why a dependency existsgo mod why module
View module dependency graphgo mod graph
Remove globally installed Go toolrm $(go env GOPATH)/bin/tool-name
Clear Go module cachego clean -modcache

These commands should be executed from the root directory of your Go project, where the go.mod file 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.

bash
go list -m all | grep gin

Example output:

text
github.com/gin-gonic/gin v1.9.1

This 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:

bash
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.

bash
go mod why github.com/gin-gonic/gin

Example output:

text
# github.com/gin-gonic/gin
example.com/myproject
github.com/gin-gonic/gin

If 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.

bash
go get github.com/gin-gonic/gin@none

This tells Go to drop the dependency from go.mod.

Step 5: Clean unused dependencies using go mod tidy

Finally, clean the dependency list.

bash
go mod tidy

This command:

  • removes unused dependencies
  • updates the go.mod file
  • updates the go.sum file

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:

bash
go get github.com/gin-gonic/gin

Go 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.

bash
go get github.com/gin-gonic/gin@none

This removes the module entry from the project's dependency graph.

Run go mod tidy to update dependencies

After removing the module, run:

bash
go mod tidy

This 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:

bash
go list -m all | grep gin

If 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:

bash
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

Go 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:

bash
go env GOBIN
go env GOPATH

If GOBIN is empty, binaries are stored in:

text
$GOPATH/bin

Example:

text
/home/user/go/bin

Remove the tool binary from $GOPATH/bin

To uninstall the tool, simply delete the binary.

Example:

bash
rm -f $(go env GOPATH)/bin/golangci-lint

This removes the executable from your system.

Verify the tool is removed from your system

You can confirm the tool is gone using:

bash
which golangci-lint

If 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:

text
github.com/example/module v1.2.3 // indirect

This 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:

bash
go mod why github.com/example/module

Example output:

text
# github.com/example/module
example.com/project
github.com/some-library
github.com/example/module

This shows which module in your project requires it.

Understand how Go rebuilds the dependency graph

Whenever you run:

bash
go mod tidy

Go 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:

text
$GOPATH/pkg/mod

Removing a dependency from a project does not delete it from the cache.

Remove cached modules from your system

To clear all cached modules, run:

bash
go clean -modcache

This 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:

bash
go mod download

or simply run:

bash
go build

Go 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:

bash
go env GOMOD

If 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:

bash
go mod tidy

Always remove dependencies using:

bash
go get module@none

Confusing Go tools with module dependencies

Some users attempt to remove CLI tools using module commands.

Remember:

ItemRemoval Method
Project dependencygo get module@none
Installed CLI tooldelete binary from $GOPATH/bin
Cached modulego clean -modcache

Clearing cache instead of removing dependencies

Running:

bash
go clean -modcache

does 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 $GOBIN or $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.

Tuan Nguyen

Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise spanning these technologies, he develops robust solutions and implements efficient data processing and management strategies across various projects and platforms.