WEBTOOLIST.
STATIC WEB PLATFORM
NO ACCOUNTS / NOTHING TO INSTALL

$ webtoolist --script docker-cleanup

Docker Cleanup Preview

Show reclaimable Docker space, then prune only with an explicit flag.

category DevOps shell Bash 4+ updated 2026-09-11
dockercleanupdiskdevops
Safety notes
  • Pruning deletes stopped containers, unused networks, dangling images, and build cache.
  • Dry run is the default; --apply is required to remove anything.
commands involved: docker prune

Dependencies: docker

Usage

  • Run without flags to see usage.
  • Add —apply to prune; add —volumes only if you are certain no data lives in anonymous volumes.

Script

#!/usr/bin/env bash
set -euo pipefail

apply=0
volumes=()

for arg in "$@"; do
  case "$arg" in
    --apply) apply=1 ;;
    --volumes) volumes+=(--volumes) ;;
    *) echo "usage: $0 [--apply] [--volumes]" >&2; exit 2 ;;
  esac
done

command -v docker >/dev/null || { echo "docker not found" >&2; exit 1; }

echo "== disk usage =="
docker system df

if ((apply)); then
  echo
  echo "== pruning =="
  docker container prune -f
  docker image prune -f
  docker network prune -f
  docker builder prune -f
  if ((${#volumes[@]})); then
    docker volume prune -f
  fi
  docker system df
else
  echo
  echo "(dry run) re-run with --apply to prune"
fi

Example

./docker-cleanup
sudo ./docker-cleanup --apply

Notes

  • Named volumes are not removed by volume prune; anonymous unused volumes are.
  • On a shared host, check with the team before pruning build cache — it slows the next build.

Related scripts

Find Large Files

Locate files above a size threshold and show them largest first.

File Management

Safe Log Cleanup

Delete rotated log files older than a set number of days, with a dry-run first.

System Administration

Disk Threshold Alert

Exit non-zero when any mounted filesystem exceeds a usage threshold.

Monitoring