405

I ran go get package to download a package before learning that I needed to set my GOPATH otherwise that package sullies my root Go install (I would much prefer to keep my Go install clean and separate core from custom). How do I remove packages installed previously?

Owen Allen
  • 11,348
  • 9
  • 51
  • 63

8 Answers8

273

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.

Matheus Felipe
  • 2,482
  • 25
  • 24
Sonia
  • 27,135
  • 8
  • 52
  • 54
  • 5
    At first I looked for $GOPATH/pkg/architecture/ which didn't exist. Then I realized that what you were referring to was $GOPATH/pkg/{{architecture}}, example $GOPATH/pkg/windows_amd64. – Owen Allen Dec 09 '12 at 23:09
  • 1
    The default value of `GOPATH` is `/usr/lib/go`. – Flimm Aug 12 '13 at 20:22
  • 415
    If it is safe and simple, why is there no go subcommand that does it? – Bengt Jun 06 '14 at 13:50
  • Just create an alias or a function for it in your shell. – thiagowfx Jul 19 '14 at 06:34
  • 163
    coming from npm, we have so much further to `go` – slf May 12 '15 at 18:58
  • On windows: %GOPATH%/pkg/windows_amd64 – Jakob Hviid PhD Jul 19 '17 at 12:54
  • 10
    On Mac: $GOPATH = $HOME/go – Ricardo Martins Nov 20 '18 at 23:15
  • 75
    The more I learn about go, the more I realize how terrible it is at doing very 'simple', 'native' stuff. I mean, how can you have a command for installing but no command to remove. How??? – Alf Moh Jun 12 '20 at 21:43
  • 5
    it's still just as safe and simple to delete the original src and package, and then clean up the direct dependencies, and then their dependencies. You will of course need to scan all the imports to determine what dependencies those are. But that's very simple isn't it. /s (lucky if you use go modules :D) – ocodo Aug 26 '20 at 06:31
  • 16
    Yeah, really, what about the dependencies ? – Hritik Oct 12 '20 at 12:43
  • 1
    It is safe? In ubuntu you have to use sudo... that is not totally safe... – Victor Apr 13 '23 at 18:39
260

A go package can be removed as follows.

go get package@none

Here @none is the version part set as none. Thus removing the package.

Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46
abhisekp
  • 4,648
  • 2
  • 30
  • 37
  • 2
    cannot use path@version syntax in GOPATH mode – A.J. Aug 12 '21 at 12:49
  • 2
    Can confirm this works for go version go1.16.5 (current date is Aug 2021). – Joshua T Aug 30 '21 at 11:32
  • This removed compiled binary from `~/go/bin`, but package still remains in `~/go/pkg/mod/github.com/...`. Is there another step? – Zach Young Jan 04 '22 at 17:06
  • 14
    (osx) didn't remove the binary from `~/go/bin` for me. no errors, no output from the command. – Andrew Jan 05 '22 at 05:09
  • 3
    @ZachYoung Try `go clean -cache -modcache`. – rodrigocfd Feb 25 '22 at 13:54
  • 1
    @rodrigocfd, thanks, that definitely cleared everything... I think I'm coming to terms with the fact that go's approach to packages is just different than what I've experienced in the past. I followed the comment on the OP, and am reading up on `go mod tidy` from the Blog. – Zach Young Feb 25 '22 at 19:00
  • 1
    nope, not working - cli still exists (i tried this with `ko`) – AthulMuralidhar Mar 16 '22 at 10:28
  • After a "go install" it gives "go: can't request version "none" of the main module (hello)". I use asdf to install go and "go clean -i mypackagename" seems to work to remove installe exec – Eric Burel Sep 23 '22 at 12:20
  • 8
    This is a weird way to remove a package. Wish it was just `uninstall` – JohnAllen Nov 03 '22 at 06:24
  • Works but this apparently doesn't remove related lines from `go.sum`. Need `go mod tidy` for it – Levi Jul 04 '23 at 09:34
  • I swear, go has some of the weirdest command line APIs out there – TheMechanic Jul 15 '23 at 19:34
  • 1
    Nowadays you have to install global packages using `go install`, but `go install ...@none` doesn't work. `go install` installs just a binary, so `rm "$(which package-name)"` is enough. – Martin Braun Aug 14 '23 at 16:53
228

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.

ches
  • 6,382
  • 2
  • 35
  • 32
51

You can use go mod tidy to clean unused packages

R0Y3R
  • 619
  • 4
  • 2
  • 4
    This works, but only to remove modules that are no longer being referenced from code in your module, using Go version >= 1.11+. To remove go programs or binaries installed with go get or go install, outside of your go module, then you have to use go clean – tothemario Feb 17 '22 at 06:37
12
#!/bin/bash

goclean() {
 local pkg=$1; shift || return 1
 local ost
 local cnt
 local scr

 # Clean removes object files from package source directories (ignore error)
 go clean -i $pkg &>/dev/null

 # Set local variables
 [[ "$(uname -m)" == "x86_64" ]] \
 && ost="$(uname)";ost="${ost,,}_amd64" \
 && cnt="${pkg//[^\/]}"

 # Delete the source directory and compiled package directory(ies)
 if (("${#cnt}" == "2")); then
  rm -rf "${GOPATH%%:*}/src/${pkg%/*}"
  rm -rf "${GOPATH%%:*}/pkg/${ost}/${pkg%/*}"
 elif (("${#cnt}" > "2")); then
  rm -rf "${GOPATH%%:*}/src/${pkg%/*/*}"
  rm -rf "${GOPATH%%:*}/pkg/${ost}/${pkg%/*/*}"
 fi

 # Reload the current shell
 source ~/.bashrc
}

Usage:

# Either launch a new terminal and copy `goclean` into the current shell process, 
# or create a shell script and add it to the PATH to enable command invocation with bash.

goclean github.com/your-username/your-repository
ecwpz91
  • 1,527
  • 1
  • 13
  • 8
1

Man, i got same problem yesterday. Could't find anything in $GOPATH/pkg/<architecture> . Then, i realized that there was a go directory in my $HOME. So, i moved to $HOME/<username>/go/pkg/mod/github.com and saw all package i had installed from github by go get

dongocanh96
  • 99
  • 1
  • 7
1

I deleted the whole go folder in my home directory and then with go mod tidy I redownloaded all dependencies actually used by my project (I had a bunch of old versions in addition to the actual versions used). I recovered about 3GB of disk space.

-4

To remove a dependency on a module and downgrade modules that require it:

go get example.com/mod@none

source: run go help get.

bfontaine
  • 18,169
  • 13
  • 73
  • 107
ssp4all
  • 371
  • 2
  • 11