Monday, March 11, 2019

How to remove package installed with go get

Let's say you have installed a Go package


$ go get github.com/mikefarah/yaml

and you want to uninstall it.

What Is Actually Installed?


When you run go get, the source code is stored in $GOPATH/src. Then the package is built and written to $GOPATH/pkg/<architecture> and any executable binaries in $GOPATH/bin. In our case, the files are stored in


$ ls $GOPATH/src/github.com/mikefarah/yaml
Dockerfile.dev  examples  scripts
LICENSE   json_converter.go utils_test.go
Makefile  json_converter_test.go vendor
Makefile.variables merge.go  version.go
README.md  merge_test.go  version_test.go
commands_test.go mkdocs   yaml.go
data_navigator.go mkdocs.yml  yaml_test.go
data_navigator_test.go path_parser.go
docs   path_parser_test.go

There are no libraries and other compiled files in $GOPATH/pkg/darwin_amd64/github.com/mikefarah/yaml, but there is an executable in $GOPATH/bin/yaml


$ ls $GOPATH/bin/yaml
/path/to/my/GOPATH/bin/yaml

Remove binaries with go clean

Let's uninstall the package


$ go clean -i github.com/mikefarah/yaml
$ ls $GOPATH/bin/yaml
ls: /path/to/my/GOPATH/bin/yaml: No such file or directory

Remove source manually

That still leaves the source code. You can manually remove the directory


$ rm -rf $GOPATH/src/github.com/mikefarah/yaml
$ rmdir $GOPATH/src/github.com/mikefarah

Note: if you have other Go projects from the same author/organization, then the second rmdir command will naturally fail.

Removing packages installed with go get

It's safe to just delete the source directory and compiled package file. Find the source directory under $GOPATH/src and the package file under $GOPATH/pkg/<architecture>, for example: $GOPATH/pkg/windows_amd64.
--------
You can delete the archive files and executable binaries that go install (or go get) produces for a package with go clean -i importpath.... These normally reside under $GOPATH/pkg and $GOPATH/bin, respectively.
Be sure to include ... on the importpath, since it appears that, if a package includes an executable, go clean -i will only remove that and not archive files for subpackages, like gore/gocode in the example below.
Source code then needs to be removed manually from $GOPATH/src.
go clean has an -n flag for a dry run that prints what will be run without executing it, so you can be certain (see go help clean). It also has a tempting -r flag to recursively clean dependencies, which you probably don't want to actually use since you'll see from a dry run that it will delete lots of standard library archive files!
A complete example, which you could base a script on if you like:
$ go get -u github.com/motemen/gore

$ which gore
/Users/ches/src/go/bin/gore

$ go clean -i -n github.com/motemen/gore...
cd /Users/ches/src/go/src/github.com/motemen/gore
rm -f gore gore.exe gore.test gore.test.exe commands commands.exe commands_test commands_test.exe complete complete.exe complete_test complete_test.exe debug debug.exe helpers_test helpers_test.exe liner liner.exe log log.exe main main.exe node node.exe node_test node_test.exe quickfix quickfix.exe session_test session_test.exe terminal_unix terminal_unix.exe terminal_windows terminal_windows.exe utils utils.exe
rm -f /Users/ches/src/go/bin/gore
cd /Users/ches/src/go/src/github.com/motemen/gore/gocode
rm -f gocode.test gocode.test.exe
rm -f /Users/ches/src/go/pkg/darwin_amd64/github.com/motemen/gore/gocode.a

$ go clean -i github.com/motemen/gore...

$ which gore

$ tree $GOPATH/pkg/darwin_amd64/github.com/motemen/gore
/Users/ches/src/go/pkg/darwin_amd64/github.com/motemen/gore

0 directories, 0 files

# If that empty directory really bugs you...
$ rmdir $GOPATH/pkg/darwin_amd64/github.com/motemen/gore

$ rm -rf $GOPATH/src/github.com/motemen/gore
Note that this information is based on the go tool in Go version 1.5.1.