Completely remove a package installed with "go get"?
Asked Answered
V

2

26

I'm using Go 1.13.1, latest as of today.

I'm trying to completely remove a package that I installed with go get from GitHub. The go clean -i <PACKAGE_NAME> didn't seem to work, since there are files spread through, at least, these directories:

~/go/pkg/mod/github.com/<PACKAGE_NAME>
~/go/pkg/mod/cache/download/github.com/<PACKAGE_NAME>
~/go/pkg/mod/cache/download/sumdb/sum.golang.org/lookup/github.com/<PACKAGE_NAME>

Is there a way to clean everything without removing all that manually?

Vyner answered 3/10, 2019 at 18:9 Comment(8)
Curios: Why? Are the files causing a problem?Gelsenkirchen
Why would git clean effect the go cache?Barbarize
@Flimzy It makes sense to me that, if I remove a package, all its files are removed.Vyner
@Barbarize No, the files are not stored in the repository folder, they're buried within ~/go.Vyner
@Rodrigo: So no real reason, then? Then don't worry about it. The cache files stick around, but do no harm. The only way to really clean them up is to delete your caches, which you can do, of course, but then you'll hurt build performance for other packages next time. The same thing happens when you go to a web site. Just because you navigate away, or delete a bookmark, doesn't mean the cached pages are removed.Gelsenkirchen
@Rodrigo: the command you put here is git clean -i. If you meant go clean -i, the help output says nothing about removing the cache: The -i flag causes clean to remove the corresponding installed archive or binaryBarbarize
@Barbarize Oops... you're right, I meant go clean. Fixed.Vyner
Possible duplicate of Removing packages installed with go getIrruption
K
47

This is currently not supported. If you think about it: it may be the current module does not need it anymore, but there may be other (unrelated) modules on your system that may still need it. The module cache is "shared" between all the modules on your system; it can be shared because dependencies are versioned, and if 2 unrelated modules refer to the same version of a module / package, it's the same and can be shared.

The closest is go clean with -modcache, but that removes the entire module cache:

The -modcache flag causes clean to remove the entire module download cache, including unpacked source code of versioned dependencies.

Kaleidoscopic answered 3/10, 2019 at 19:16 Comment(5)
Any idea on why this approach was taken, instead of NPM's approach? Anyway, it would be nice to have a tool to analyze the cache and cleanup orphan packages.Vyner
@Rodrigo It's in the answer: because the module cache is "shared", common to all modules. You can't write a tool to analyze the module cache, because modules referring / needing packages in the cache might be literally anywhere in your computer, even on a flash drive that is not even connected at all times to your system.Kaleidoscopic
Coming from the npm world myself, I find the way go manages packages maddeningly frustrating. Managing packages on a per-application basis makes a lot more sense to me, and it seems to be the direction the world is going: tools like pyenv and nvm make it easy to manage not only packages but also the entire stack on a per-application basis.Connote
@LaneRettig Personally, I prefer globally managing multiple versions compared to a per-application basis, I don't want ~GBs of go_modules sitting in each project. Though I agree there should be a way provided out of the box to remove these dependencies installed globally.Pinder
Just gonna say, I don't personally use Rust, only for some small pet projects, but the experience of the cargo tool and managing dependencies in Rust is really nice. Haven't had any issues with the setup, installing, cleaning. But the compile times/ installations do get annoyingly long, so pick your poison...Coinsurance
T
4

We can remove cache for one or multiple packages from GOPATH easily.

  1. Delete go.sum file in your project root folder.
  2. Delete vcs folder in GOPATH/pkg/mod/cache.
  3. Delete all files of your library in GOPATH/pkg/mod/cache/download/{Package_name}/{library_name} or remove files ({version_to_delete}.*) belongs to specific version and update list file.
  4. Delete specific version of library in GOPATH/pkg/mod/{Package_name}/{library_name}@{version}.
  5. Now, run go mod tidy in your project root folder. It should download library from the internet instead of respawing from local cache.
Tiossem answered 16/5, 2023 at 2:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.