#!/usr/bin/env bash set -euo pipefail # Usage: # ./scripts/cleanup-docker.sh # safe cleanup (dangling images, stopped containers, build cache) # ./scripts/cleanup-docker.sh --all # also prune unused images # ./scripts/cleanup-docker.sh --volumes # also prune unused volumes (DANGEROUS) # ./scripts/cleanup-docker.sh --nuke-box # remove known box containers + named volumes (VERY DANGEROUS) ALL=0 VOLUMES=0 NUKE_BOX=0 for arg in "${@:-}"; do case "$arg" in --all) ALL=1 ;; --volumes) VOLUMES=1 ;; --nuke-box) NUKE_BOX=1 ;; *) echo "Unknown arg: $arg" >&2; exit 2 ;; esac done echo "== docker cleanup starting ==" echo "== pruning stopped containers ==" docker container prune -f >/dev/null || true echo "== pruning build cache ==" docker builder prune -f >/dev/null || true echo "== pruning dangling images ==" docker image prune -f >/dev/null || true if [ "$ALL" -eq 1 ]; then echo "== pruning unused images (--all) ==" docker image prune -af >/dev/null || true fi if [ "$VOLUMES" -eq 1 ]; then echo "== pruning unused volumes (--volumes) ==" docker volume prune -f >/dev/null || true fi if [ "$NUKE_BOX" -eq 1 ]; then echo "== removing known box containers (--nuke-box) ==" docker rm -f box-mgnt-api box-app-api box-stats-api box-mysql box-mongodb box-rabbitmq box-redis 2>/dev/null || true echo "== removing known box volumes (--nuke-box) ==" docker volume rm box_mongo_data box_redis_data box_rabbitmq_data box_mysql_data 2>/dev/null || true echo "== removing box network (--nuke-box) ==" docker network rm box-network 2>/dev/null || true fi echo "== docker cleanup done ==" docker system df || true