Skip to content
shellmap

Clean the package manager cache

Free disk space taken by downloaded `.deb` / `.rpm` / package archives the package manager keeps after install. Critical on small VMs, Docker images, and CI runners that accumulate hundreds of MB of cached archives across builds.

How to clean the package manager cache in each shell

Bashunix
sudo apt clean

Debian/Ubuntu: `apt clean` deletes EVERYTHING from `/var/cache/apt/archives/` (every `.deb` ever downloaded, plus partials). `apt autoclean` is gentler — only removes `.deb` files for packages that are no longer available from any configured repo (i.e. obsolete). For a "deep clean": `sudo apt clean && sudo apt autoremove --purge` (also removes orphaned deps + their config files). Distro variants: `sudo dnf clean all` (Fedora/RHEL — clears `/var/cache/dnf/`), `sudo pacman -Sc` (Arch — keeps the latest version of each currently-installed package, drops the rest), `sudo pacman -Scc` (DOUBLE-c — drops EVERYTHING including currently-installed-version archives; can break partial-rollback workflows), `brew cleanup` (macOS — removes old versions and unused deps), `sudo apk cache clean` (Alpine).

Zshunix
sudo apt clean
Fishunix
sudo apt clean

No fish-side wrinkle. CAVEAT: when chaining `apt clean && apt autoremove`, fish 3.0+ handles `&&` natively; older fish needs `; and`. For idiomatic fish: `sudo apt clean; and sudo apt autoremove --purge -y`. If you alias the combo, define a fish function in `~/.config/fish/functions/apt-deep-clean.fish` so it survives shell restarts (don't alias inside `config.fish` — fish functions are the idiomatic form).

PowerShellwindows
winget source reset --force

`winget source reset` clears the local source-index cache (the manifest list winget reads to know what packages exist). For DOWNLOADED installers winget does NOT keep a long-term cache — each install streams from the publisher's URL directly, so there is no `winget clean` analogue. Caches that DO grow on Windows: `%LOCALAPPDATA%\Temp\WinGet\` (transient install scratch — safe to delete: `Remove-Item -Recurse "$env:LOCALAPPDATA\Temp\WinGet"`). PowerShellGet module cache: `Remove-Item -Recurse "$env:LOCALAPPDATA\Microsoft\Windows\PowerShell\PowerShellGet\"`. NuGet cache (often gigabytes for devs): `dotnet nuget locals all --clear`.

cmd.exewindows
winget source reset --force

Same `winget.exe`. Chocolatey caches DO accumulate — `choco-cleaner` (community tool: `choco install choco-cleaner -y && choco-cleaner`) sweeps `C:\ProgramData\chocolatey\lib-bad`, `lib-bkp`, old logs, and the `.nupkg` cache. Direct: `Remove-Item C:\ProgramData\chocolatey\lib-bad\* -Recurse -Force` and `Remove-Item C:\ProgramData\chocolatey\logs\*.log -Force`. Scoop: `scoop cache rm *` removes the download cache (which is otherwise kept indefinitely under `~\scoop\cache\` — useful for offline reinstall but can hit several GB).

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • **`apt clean` vs `apt autoclean` vs `apt autoremove` — three different cleanups, often confused.** `apt clean`: deletes ALL `.deb` archives from `/var/cache/apt/archives/` and partials from `/var/cache/apt/archives/partial/`. Frees up to "everything you ever installed × archive size". Safe — installed packages don't need their archives. `apt autoclean`: deletes only `.deb` files whose package is no longer available from any configured repo — narrower scope. `apt autoremove`: removes installed packages that were pulled in as auto-installed deps and are no longer needed. Different KIND of cleanup — touches the INSTALL set, not the archive cache. `apt autoremove --purge` also wipes their `/etc/<pkg>/` configs. Recipe for the smallest Debian/Ubuntu footprint: `sudo apt autoremove --purge -y && sudo apt clean && sudo rm -rf /var/lib/apt/lists/*` (last command also drops the index files; you must `apt update` before the next install).
  • **Arch `pacman -Sc` vs `pacman -Scc` and `paccache` — keep-N-versions discipline.** `pacman -Sc` (one `c`): removes from `/var/cache/pacman/pkg/` only archives for packages no longer installed; keeps everything else including old versions. `pacman -Scc` (two `c`s): removes EVERYTHING. Dangerous if you ever need to roll back — Arch is rolling and an upgrade that breaks userspace is sometimes only recoverable with `pacman -U /var/cache/pacman/pkg/<old-version>.pkg.tar.zst`. The recommended Arch cleanup is `paccache -r` (from `pacman-contrib`) which keeps the LAST 3 versions of every installed package by default — tunable with `-k <N>`. Cron-style: `paccache -rk1` for a tight box (keep 1 prior version). Always inspect with `paccache -dvk2` (dry-run keeping last 2) before letting it remove things.
  • **`brew cleanup` and the new Homebrew default.** `brew cleanup` removes old versions of installed formulae from `/usr/local/Cellar/` (Intel) / `/opt/homebrew/Cellar/` (Apple Silicon), purges stale download caches in `~/Library/Caches/Homebrew/`, and clears outdated brew logs. Homebrew autoruns this every 30 days by default (since 4.0+) — you can override via `HOMEBREW_NO_INSTALL_CLEANUP=1` in your shell rc to STOP auto-cleanup, or `HOMEBREW_CLEANUP_PERIODIC_FULL_DAYS=7` to make it weekly. For a deep clean: `brew cleanup --prune=all` (also removes the download cache for any version including currently installed). `brew autoremove` (separate command) handles "remove deps no longer required" — the apt-autoremove analogue.
  • **Docker image package-cache hygiene — the `--no-install-recommends` and single-RUN idiom.** In a Dockerfile, EVERY layer is permanent — so an `apt-get install` layer followed by an `apt-get clean` layer ADDS the clean step but the install layer still holds the cache. Combine in ONE `RUN`: `RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/*`. The `rm -rf /var/lib/apt/lists/*` is critical — without it the apt index files (~50MB) bloat the image. `--no-install-recommends` skips suggested-but-optional deps; on a server-side image (no GUI deps needed) this often halves the image size. Fedora analogue: `RUN dnf install -y curl && dnf clean all && rm -rf /var/cache/dnf`. Alpine analogue: `RUN apk add --no-cache curl` (the `--no-cache` flag bakes the cleanup INTO `apk add`).
  • **Free-space audit before deciding what to clean.** Don't guess — measure. Debian/Ubuntu: `du -sh /var/cache/apt/archives /var/lib/apt/lists` shows the two big apt caches. Fedora: `du -sh /var/cache/dnf`. Arch: `du -sh /var/cache/pacman/pkg`. Homebrew: `du -sh "$(brew --cache)"`. Often the bigger wins are NOT in the package cache: `journalctl --disk-usage` (systemd journal — `sudo journalctl --vacuum-time=2weeks` trims to 2-week retention), Docker images and containers (`docker system df` then `docker system prune -af --volumes`), `/tmp` (cleaned on reboot but can fill mid-session — `sudo find /tmp -type f -atime +14 -delete`), and developer caches (`~/.cache/`, `~/.npm/`, `~/.cargo/registry/cache/`, `~/.gradle/caches/`). The "clean the package cache" reflex is usually NOT the largest space win — measure first, target the largest.

Related tasks