How to remove an installed package using go modules
Asked Answered
H

3

65

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?

Hayleyhayloft answered 24/7, 2019 at 15:42 Comment(0)
H
131

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

Hayleyhayloft answered 24/7, 2019 at 15:57 Comment(4)
Module not in go.mod but still in vendor dir!Spoilfive
@Spoilfive have you execute it with the GOFLAG -mode=vendor? github.com/golang/go/wiki/…Hayleyhayloft
Seems to remove code generating modules (that aren't used directly in the code, only in //go:generate comments) is there a way to preserve those modules?Dipole
This does not remove modules in go.sumIslander
B
6

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

Bourg answered 28/6, 2021 at 11:10 Comment(2)
Also run go clean -modcache to remove module cache.Drusilladrusus
For yuks, you might want to verify that $(go env GOPATH) doesn't return something empty, or otherwise awful.Chaise
I
3

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

Illegitimacy answered 27/8, 2023 at 10:49 Comment(1)
This is the right commandVandalism

© 2022 - 2024 — McMap. All rights reserved.