What is the difference between these package cache clearing methods?

When building Docker images using ubuntu, some devs recommend removing package caches to reduce the size of the generated image by running the following command:

rm -rf /var/lib/apt/lists/*

However, there is also the command apt-get clean which removes the package cache located at /var/cache/apt/archives (reference: https://help.ubuntu.com/community/AptGet/Howto)

Why do devs recommend the former command over the latter? Is there anything to gain by running both commands since they target different cache locations?


Solution 1:

They do different things. rm -rf /var/lib/apt/lists/* removes package lists, loaded by apt update. apt clean removes cached packages, loaded by apt install or apt upgrade. Both require root permissions (sudo).

Solution 2:

The rm command would require sudo. So a simple typo or hitting Enter prematurely would irreparably destroy your system. There is absolutely no reason not to use sudo apt clean for this purpose.