clean unused packages in $GOPATH/pkg/mod

2,285 views
Skip to first unread message

william_ge

unread,
Jul 6, 2020, 12:46:52 PM7/6/20
to golang-nuts
Is there any way to clean unused packages in go mod directory?
currently when you use go mod to manage you dependencies,new packages are added  and old ones are not removed.

So can we maintain a global gomod.stats file to keep reference counter of every package? It can only be modified when executing `go mod download/init/tidy` and when  running `go mod clean`,  it will traverse gomod.stats and find packages who's refcount is 0 then delete them.

This is similar to simplified GC using reference counting and I think customizing digraph may deal with it.

this is  golang.org/x/tools packages in my GOPATH/pkg/mod, totally 32 directories with 41463 files and 16185 sub-directories. It's not necessary now but I think wo need it sooner or later. 
``` 
D:\~\go\pkg\mod\golang.org\x>dir  | findstr tools
2020/07/02 周四  19:36    <DIR>          tools
2020/01/09 周四  23:02    <DIR>          to...@v0.0.0-20180824175216-6c1c5e93cdc1
2020/01/09 周四  23:03    <DIR>          to...@v0.0.0-20181130195746-895048a75ecf
2019/11/12 周二  15:00    <DIR>          to...@v0.0.0-20190312170243-e65039ee4138
2020/01/09 周四  23:02    <DIR>          to...@v0.0.0-20190408220357-e5b8258f4918
2020/04/24 周五  14:47    <DIR>          to...@v0.0.0-20190611222205-d73e1c7e250b
2019/11/20 周三  15:46    <DIR>          to...@v0.0.0-20190809145639-6d4652c779c4
2019/12/13 周五  17:16    <DIR>          to...@v0.0.0-20191012152004-8de300cfc20a
2020/01/09 周四  23:03    <DIR>          to...@v0.0.0-20191030062658-86caa796c7ab
2019/11/19 周二  14:59    <DIR>          to...@v0.0.0-20191108194844-46f05828f2fe
2019/11/19 周二  14:59    <DIR>          to...@v0.0.0-20191118222007-07fc4c7f2b98
2020/01/09 周四  23:03    <DIR>          to...@v0.0.0-20191125144606-a911d9008d1f
2019/12/11 周三  16:41    <DIR>          to...@v0.0.0-20191206201009-952e2c076240
2019/12/11 周三  16:41    <DIR>          to...@v0.0.0-20191210221141-98df12377212
2020/01/09 周四  23:02    <DIR>          to...@v0.0.0-20200108203644-89082a384178
2020/02/28 周五  10:24    <DIR>          to...@v0.0.0-20200130002326-2f3ba24bd6e7
2020/02/18 周二  16:59    <DIR>          to...@v0.0.0-20200212213342-7a21e308cf6c
2020/02/18 周二  16:59    <DIR>          to...@v0.0.0-20200216192241-b320d3a0f5a2
2020/04/30 周四  15:41    <DIR>          to...@v0.0.0-20200226224502-204d844ad48d
2020/03/03 周二  12:29    <DIR>          to...@v0.0.0-20200227200655-6862ededa516
2020/02/28 周五  10:23    <DIR>          to...@v0.0.0-20200227222343-706bc42d1f0d
2020/03/03 周二  12:29    <DIR>          to...@v0.0.0-20200302225559-9b52d559c609
2020/06/15 周一  22:39    <DIR>          to...@v0.0.0-20200304193943-95d2e580d8eb
2020/03/25 周三  16:50    <DIR>          to...@v0.0.0-20200316194252-fafb6e2e8a4a
2020/03/25 周三  16:50    <DIR>          to...@v0.0.0-20200325010219-a49f79bcc224
2020/04/10 周五  17:48    <DIR>          to...@v0.0.0-20200407041343-bf15fae40dea
2020/04/10 周五  17:48    <DIR>          to...@v0.0.0-20200410040751-3bd20875a2eb
2020/04/30 周四  15:40    <DIR>          to...@v0.0.0-20200430040329-4b814e061378
2020/05/11 周一  21:46    <DIR>          to...@v0.0.0-20200509030707-2212a7e161a5
2020/05/15 周五  09:43    <DIR>          to...@v0.0.0-20200513154647-78b527d18275
2020/05/15 周五  09:43    <DIR>          to...@v0.0.0-20200515010526-7d3b6ebf133d
2020/07/02 周四  19:36    <DIR>          to...@v0.0.0-20200701133321-6ddc6be4d35f
2020/07/02 周四  19:36    <DIR>          to...@v0.0.0-20200702044944-0cc1aa72b347
```

Thanks.

Amnon

unread,
Jul 7, 2020, 1:26:16 AM7/7/20
to golang-nuts
Just delete them all.
Your next build will repopulate the ones that are needed.

HailangGe

unread,
Jul 7, 2020, 3:23:50 AM7/7/20
to golang-nuts

Thanks. I'm just trying to look for if there are any way to clean them in a programmer's way.

seank...@gmail.com

unread,
Jul 7, 2020, 5:51:17 AM7/7/20
to golang-nuts
go clean -modcache

HailangGe

unread,
Jul 7, 2020, 12:49:31 PM7/7/20
to golang-nuts
> go clean -modcache
It seems that the only way is to clean all packages.

Thank you very much!
Ge

Reply all
Reply to author
Forward
0 new messages