jesugmz
jesugmz

Reputation: 2660

How to remove an installed package using go modules

I've installed a package using go modules (go get in Go 1.13) and now I want to remove it. In the documentation there is nothing about this and in go get docu neither.

Removing the package from go.mod manually doesn't solve the issue so it remains in go.sum.

How should I remove a package in a clean way?

Upvotes: 73

Views: 116780

Answers (3)

Leon Africa
Leon Africa

Reputation: 599

If you used go install package@latest then to remove:

go install package@none

go clean -cache -modcache

When in VS Code CTRL+SHIFT+P and select GO: Restart Language Server

Upvotes: 8

jesugmz
jesugmz

Reputation: 2660

Found it https://go.dev/blog/using-go-modules#removing-unused-dependencies

go mod tidy

So basically, once the package is not being imported in any package you can perform a go mod tidy and it will safely remove the unused dependencies.

And if you are vendoring the dependencies, then run the command below to make the module changes be applied in the vendor folder:

go mod vendor

Upvotes: 143

itismoej
itismoej

Reputation: 1847

@jesugmz answer doesn't say, what if you wanna remove a currently using package in go modules.

So, if you're using go modules (you have a go.mod file in your project) and you want to remove a currently using package, check $GOPATH/pkg/mod/ directory and simply remove the package named package@version.

For example, if you have github.com/some/project package installed, you should run the following command:

rm -rf $(go env GOPATH)/pkg/mod/github.com/some/[email protected]

You can find the using package version in go.mod file.

Upvotes: 6

Related Questions