22

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?

icza
  • 389,944
  • 63
  • 907
  • 827
rodrigocfd
  • 6,450
  • 6
  • 34
  • 68
  • 1
    Curios: Why? Are the files causing a problem? – Jonathan Hall Oct 03 '19 at 18:15
  • 1
    Why would `git clean` effect the go cache? – JimB Oct 03 '19 at 18:38
  • @Flimzy It makes sense to me that, if I remove a package, all its files are removed. – rodrigocfd Oct 03 '19 at 18:44
  • @JimB No, the files are not stored in the repository folder, they're buried within `~/go`. – rodrigocfd Oct 03 '19 at 18:45
  • @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. – Jonathan Hall Oct 03 '19 at 18:46
  • @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 binary` – JimB Oct 03 '19 at 18:49
  • @JimB Oops... you're right, I meant `go clean`. Fixed. – rodrigocfd Oct 03 '19 at 18:55
  • Possible duplicate of [Removing packages installed with go get](https://stackoverflow.com/questions/13792254/removing-packages-installed-with-go-get) – Hossein Oct 03 '19 at 20:33

2 Answers2

37

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.

icza
  • 389,944
  • 63
  • 907
  • 827
  • 3
    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. – rodrigocfd Oct 03 '19 at 19:27
  • @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. – icza Oct 03 '19 at 19:33
  • 13
    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. – Lane Rettig Jun 18 '20 at 17:32
  • @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. – Phani Rithvij Nov 06 '21 at 15:33
  • 1
    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... – Nikola-Milovic Jan 22 '23 at 08:52
0

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.
Bala555
  • 119
  • 1
  • 6