32

I want to know if there is golang way to delete or remove a go package. To be simpler, opposite to go get command.

e.g. 
$ go get <PACKAGE_NAME> 
$ go delete <PACKAGE_NAME>    (Looking for similar functionality)

Please NOTE that I am aware that we can remove the downloaded files from src and pkg directory manually by using rm command. But the problem is that system command rm is not aware of your go specific stuffs (etc. to look inside $GOPATH/pkg .. ) and this is an extra step for the user while using rm . I am looking for something which all package managers provides.. as mentioned in one comment.. (npm uninstall, pip uninstall etc..).

Sitesh
  • 1,816
  • 1
  • 18
  • 25
  • 2
    It's hard to get simpler than "rm". – Jonathan Hall Oct 29 '18 at 15:39
  • The problem with rm is that we have to identify all the files manually which can be tedious at times.. In your view "go get" also can be simply replaced by a cURL or something similar.. :) I wanted to know if the support is provided by golang itself.. – Sitesh Oct 29 '18 at 15:43
  • 2
    `rm -r $GOPATH/src/package` should do it. No need to manually identify anything. There's no equivalent replacement for `go get`. – Jonathan Hall Oct 29 '18 at 15:44
  • If I am not wrong "go get" downloads (clones) the required repository as well which also should be cleaned up when you delete the package. And deleting both are not same as running a single command like go get.. to be more precise I am looking for a counterpart of go get.. – Sitesh Oct 29 '18 at 15:45
  • 2
    For git (and AFAIK, all other popular/supported RCSs), their meta data is stored within the checked-out directory, so I don't see any problem here. – Jonathan Hall Oct 29 '18 at 15:47
  • Do you mean the meta data is not needed once the package is built ? – Sitesh Oct 29 '18 at 17:00
  • 1
    The meta data isn't need ever, unless you wish to interact with the RCS (i.e. do a 'git pull'). It's never used for package building. I'm not really sure what you're asking. – Jonathan Hall Oct 29 '18 at 18:11
  • 1
    A comment on your recent edit: I'm not sure what distinction you're trying to draw. You say "I am aware that we can remove the downloaded files from src and pkg directory manually by using rm command", but then appear to be contrasting that with "But is there a simple way to delete the downloaded package". Do you see these as different things? Because to me they seem to be the same. – Jonathan Hall Oct 29 '18 at 18:14
  • 12
    I am a bit surprised that there are so many downvotes. I think this is a perfectly valid question as other package manager do provide utilities to uninstall installed packages, e.g. `gem uninstall`, `npm uninstall` or `pip uninstall` – mbuechmann Oct 29 '18 at 19:23
  • @Flimzy basically I meant if there is golang way of removing the pkg as provided by other package managers. – Sitesh Oct 30 '18 at 00:45
  • @mbuechmann You are right.. there is no respect for a simple question in SO even if they don't have a solution/answer. – Sitesh Oct 30 '18 at 06:59
  • 3
    Possible duplicate of [Removing packages installed with go get](https://stackoverflow.com/questions/13792254/removing-packages-installed-with-go-get) – Nik Apr 08 '19 at 11:08

4 Answers4

15

You can just delete it from you disk:

rm -r $GOPATH/src/<PACKAGE_NAME>

This will remove the package completely. Alas, there is no tool or go command for removing packages. But this should be simple enough.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
mbuechmann
  • 5,413
  • 5
  • 27
  • 40
  • 3
    What if the package installs files into $GOPATH/pkg or $GOPATH/bin? And how would you know if it did? – Mad Wombat Oct 29 '18 at 21:33
  • 1
    @MadWombat: "The package" won't install files there--but the go compiler might. There's no automatic way to remove those. Removing $GOPATH/pkg files would be as simple as a second rm command such as `rm -r $GOPATH/pkg/*/`. There is no way to tie the contents of `$GOPATH/bin` back to the source package, though--as multiple packages could create the same output name. – Jonathan Hall Oct 30 '18 at 06:22
  • 17
    So the `rm -r $GOPATH/src/` command in your answer does not, in fact, remove the package completely. And what you seem to be saying is that there is no way to remove an installed package completely. That is a sad state of affairs for a language that is supposed to be "modern" – Mad Wombat Oct 30 '18 at 15:25
  • `go clean -i ` seems to do the job at the moment. – dtrckd Apr 07 '20 at 21:35
10

But is there a simple way to delete the downloaded package (go delete PACKAGE_NAME?) ?

No there is not.

Manually rming is the way to go.

Volker
  • 40,468
  • 7
  • 81
  • 87
4

For those using Go Modules rm is not enought as your go.mod and go.sum files stays dirty. You can use go mod tidy command which works but is not so intuitive.

From https://blog.golang.org/migrating-to-go-modules:

Removing a dependency can only be done after checking all packages in a module, and all possible build tag combinations for those packages. An ordinary build command does not load this information, and so it cannot safely remove dependencies.

The go mod tidy command cleans up these unused dependencies

The first time I used it removed unused dependencies but also installed new ones. Later I discovered that it run tests to check if a dependency can be removed as described in https://github.com/golang/go/wiki/Modules#why-does-go-mod-tidy-record-indirect-and-test-dependencies-in-my-gomod.

guizo
  • 2,594
  • 19
  • 26
1

I found an easy solution for doing this. I am using fish shell so I just added this simple function in ~/.config/fish/config.fish.

It does the job perfectly with cleaning up empty folders under $GOPATH. You might want to remove this part if you want to keep empty folders but for me, it was just annoying.

Note that your GOPATH must be declared before the function declaration or this will not work.

Make sure that you are checking for empty args because otherwise, this would remove the whole $GOPATH/src folder and you do not want that.

If you're not under Mac, you'd need to edit the 'darwin_amd64' folder and if you're not on fish, this will not work but you can simply modify it to fit your current shell.

export GOPATH=$HOME/Go

function godel
   if string length -q $argv
      rm -rf $GOPATH/src/$argv $GOPATH/pkg/darwin_amd64/$argv.a
      find $GOPATH -type d -empty -delete
   else
      echo you must send an argument
   end
end
Claude Roy
  • 147
  • 2
  • 5