Skip to content
Latchkey

apt-get clean and Cleaning apt Lists in Docker

apt-get clean removes downloaded .deb archives and deleting /var/lib/apt/lists drops the package index, trimming Docker image size.

After an install in a Dockerfile, the apt cache and lists are dead weight in the final image. Clean them in the same RUN so they never enter a layer.

What it does

apt-get clean empties /var/cache/apt/archives of downloaded package files. Deleting /var/lib/apt/lists/* removes the fetched index (you will need apt-get update again before the next install). apt-get autoremove drops dependencies no longer needed.

Common usage

Dockerfile
RUN apt-get update && \
    apt-get install -y --no-install-recommends curl ca-certificates && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

Options

CommandWhat it does
apt-get cleanDelete downloaded .deb archives from the cache
apt-get autocleanDelete only archives that can no longer be downloaded
apt-get autoremoveRemove automatically-installed deps no longer required
rm -rf /var/lib/apt/lists/*Remove the fetched package index (saves space)

In CI

Do the install, clean, and lists removal in one RUN so the intermediate cache never lands in an image layer. If you use BuildKit cache mounts (--mount=type=cache,target=/var/cache/apt), do not rm the lists inside the mount; keep the cache warm across builds instead.

Common errors in CI

An install that fails with "Unable to locate package" right after rm -rf /var/lib/apt/lists/* means you deleted the index and did not run apt-get update again. If image size does not drop, the clean ran in a separate RUN from the install, so the archives are already committed in an earlier layer.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →