Trimming pip cache? (distinct from purging)

I’m running low on space so want to get rid of unnecessary files. I know that I can completely clean out the pip cached files with pip cache purge but I don’t necessarily want to download the packages again. Is there way to remove everything except the most recent versions from the cache? What about removing caches for everything that isn’t currently installed?

Which pip cache do you have in mind? Cleaning out the HTTP cache is all-or-nothing, but you can list individual entries in the wheel cache with pip cache list and delete them with pip cache remove <packagename>.

Oh, it was the http cache I was thinking of since it has the largest volume. Thanks for the letting me know it’s all or nothing.

You could get the directory from pip cache dir and then manually delete the http directory from there.

$ pip cache dir
/Users/hugo/Library/Caches/pip
$ du -sh /Users/hugo/Library/Caches/pip/*
259M	/Users/hugo/Library/Caches/pip/http
272K	/Users/hugo/Library/Caches/pip/selfcheck
4.0K	/Users/hugo/Library/Caches/pip/selfcheck.json
8.7M	/Users/hugo/Library/Caches/pip/wheels

Do the pip maintainers think it would be worthwhile adding an extra subcommand or option to specify which cache to pip cache purge?

For example: pip cache purge http or pip cache purge --dir http ?

There’s an open PR for including the HTTP cache in the things that pip cache affects.

Since 20.3, the http directory is already included in pip cache info and pip cache purge:

There’s this open PR to also deal with empty subdirectories, but it’s unrelated:

My question was rather about an option to, for example, only delete the http cache, or only delete the wheels cache.