Skip to content
Latchkey

apt-get autoremove: Usage, Options & Common CI Errors

apt-get autoremove deletes packages that were pulled in as dependencies and are no longer needed.

apt-get autoremove cleans up "orphaned" dependencies - packages installed automatically for something that is now gone. It is a standard image-slimming step, with one caveat about what apt considers automatically installed.

What it does

apt-get autoremove removes packages that were installed as dependencies of other packages and are no longer required by anything currently installed. Adding --purge also deletes their config files. It is commonly run after removing build-time packages to reclaim space in a Docker layer.

Common usage

Terminal
apt-get autoremove -y
apt-get autoremove --purge -y                     # also drop their config
apt-get remove -y build-essential && apt-get autoremove -y
apt-get autoremove -y && rm -rf /var/lib/apt/lists/*

Common errors in CI

The trap is what apt thinks is "automatically installed": a package you installed explicitly is marked manual and is safe, but one pulled in as a dependency can be swept away unexpectedly - read the "The following packages will be REMOVED" list. If a build later fails with a missing tool you assumed was present, autoremove may have taken it because nothing currently installed depended on it. Mark anything you want kept with apt-mark manual. Run it in the same RUN layer as the removal so the savings persist.

Options

FlagWhat it does
-y / --yesAssume yes (required in CI)
--purgeAlso remove config files of orphans
apt-mark manual <pkg>Protect a package from autoremove

Related guides

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