Tuesday, October 29, 2024

Golang clean package cache

Cleaning golang (cache)packages on your system

The usual flow is to install packages on your system with the simple command go install url/package@version, for example to install godoc you will run go install golang.org/x/tools/cmd/godoc@latest.

This works well untill you kind of run of disk space …

So how to clean up your disk space ?

First you need to know where the packages are downloaded that is under your $GOMODCACHE this you can find by running

go env and is usually under $HOME/go/pkg/mod.

On my system this looks like this before clean up.

$ cd $HOME/go/pkg/mod
# list size
$ du -sh .
9.6G  # space used

To clean up

# clean the cache
$ go clean -modcache
$ cd $HOME/go/pkg/mod
# list size
$ du -sh .
4.0K

That’s it.