Skip to content

Instantly share code, notes, and snippets.

@fabiobarboza7
Last active November 5, 2025 16:15
Show Gist options
  • Select an option

  • Save fabiobarboza7/5c1442e0ed0faacd0ebd382249d61923 to your computer and use it in GitHub Desktop.

Select an option

Save fabiobarboza7/5c1442e0ed0faacd0ebd382249d61923 to your computer and use it in GitHub Desktop.
Docker Full Cleanup - LLM revised
#!/bin/bash
#
# Docker Complete Cleanup Script for macOS (Docker Desktop)
#
# WARNING: This script is DESTRUCTIVE. It will stop and remove ALL Docker
# data including containers, images, volumes, networks, build history, and cache.
# USE WITH EXTREME CAUTION.
#
################################################################################
# Don't exit on error - we'll handle errors gracefully
set +e
# --- Configuration ---
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# --- Functions ---
log() {
printf "%b\n" "$1"
}
log_info() {
log "${BLUE}ℹ ${1}${NC}"
}
log_success() {
log "${GREEN}✓ ${1}${NC}"
}
log_warning() {
log "${YELLOW}⚠ ${1}${NC}"
}
log_error() {
log "${RED}✗ ${1}${NC}"
}
# Function to safely execute a command and handle errors
safe_execute() {
local description="$1"
shift
log_info "$description"
if "$@" > /dev/null 2>&1; then
log_success "$description completed"
return 0
else
log_warning "$description failed or nothing to do"
return 1
fi
}
# --- Main Script ---
log "${RED}#############################################################${NC}"
log "${RED}# WARNING #${NC}"
log "${RED}# This script will permanently delete ALL Docker data. #${NC}"
log "${RED}# This includes: #${NC}"
log "${RED}# - All containers (running and stopped) #${NC}"
log "${RED}# - All images #${NC}"
log "${RED}# - All volumes #${NC}"
log "${RED}# - All custom networks #${NC}"
log "${RED}# - All build cache and build history #${NC}"
log "${RED}# - All buildx builders (except default) #${NC}"
log "${RED}# This is DESTRUCTIVE. There is NO undo. #${NC}"
log "${RED}#############################################################${NC}"
echo
read -p "Are you absolutely sure you want to continue? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_warning "Cleanup aborted by user."
exit 1
fi
log "\n${GREEN}User confirmed. Commencing complete Docker cleanup...${NC}"
log "----------------------------------------------------"
# Display Docker info
log_info "Docker version: $(docker --version 2>/dev/null || echo 'unknown')"
log_info "Docker Compose version: $(docker compose version 2>/dev/null || echo 'unknown')"
log ""
# Step 1: Stop all running containers
log "${YELLOW}>> Step 1: Stopping all running containers...${NC}"
RUNNING_CONTAINERS=$(docker ps -q 2>/dev/null)
if [ -n "$RUNNING_CONTAINERS" ]; then
echo "$RUNNING_CONTAINERS" | xargs docker stop 2>/dev/null || true
log_success "All running containers stopped."
else
log_info "No running containers found."
fi
# Step 2: Remove all containers (stopped and running)
log "\n${YELLOW}>> Step 2: Removing all containers...${NC}"
ALL_CONTAINERS=$(docker ps -aq 2>/dev/null)
if [ -n "$ALL_CONTAINERS" ]; then
echo "$ALL_CONTAINERS" | xargs docker rm -f 2>/dev/null || true
log_success "All containers removed."
else
log_info "No containers to remove."
fi
# Step 3: Stop and remove all buildx builders (except default)
log "\n${YELLOW}>> Step 3: Stopping active builds and removing buildx builders...${NC}"
# Get all buildx builders except default ones
BUILDX_BUILDERS=$(docker buildx ls --format '{{.Name}}' 2>/dev/null | grep -v -E '^default$|^desktop-linux$' || true)
if [ -n "$BUILDX_BUILDERS" ]; then
echo "$BUILDX_BUILDERS" | while read -r builder; do
if [ -n "$builder" ]; then
docker buildx stop "$builder" 2>/dev/null || true
docker buildx rm "$builder" 2>/dev/null || true
fi
done
log_success "Non-default buildx builders removed."
else
log_info "No additional buildx builders to remove."
fi
# Step 4: Remove all custom networks
log "\n${YELLOW}>> Step 4: Removing all custom networks...${NC}"
# Get all networks except default ones
CUSTOM_NETWORKS=$(docker network ls --format '{{.Name}}' 2>/dev/null | grep -v -E '^bridge$|^host$|^none$|^docker-desktop$' || true)
if [ -n "$CUSTOM_NETWORKS" ]; then
echo "$CUSTOM_NETWORKS" | while read -r network; do
if [ -n "$network" ]; then
docker network rm "$network" 2>/dev/null || true
fi
done
log_success "Custom networks removed."
else
log_info "No custom networks to remove."
fi
# Step 5: Force remove all volumes
log "\n${YELLOW}>> Step 5: Removing all volumes...${NC}"
ALL_VOLUMES=$(docker volume ls -q 2>/dev/null)
if [ -n "$ALL_VOLUMES" ]; then
echo "$ALL_VOLUMES" | xargs docker volume rm -f 2>/dev/null || true
log_success "All volumes removed."
else
log_info "No volumes to remove."
fi
# Step 6: Remove all images
log "\n${YELLOW}>> Step 6: Removing all Docker images...${NC}"
ALL_IMAGES=$(docker images -aq 2>/dev/null)
if [ -n "$ALL_IMAGES" ]; then
echo "$ALL_IMAGES" | xargs docker rmi -f 2>/dev/null || true
log_success "All images removed."
else
log_info "No images to remove."
fi
# Step 7: Clear all build cache
log "\n${YELLOW}>> Step 7: Clearing all build cache...${NC}"
docker builder prune -af 2>/dev/null || true
log_success "Build cache cleared."
# Step 8: Comprehensive system prune
log "\n${YELLOW}>> Step 8: Running comprehensive system prune...${NC}"
docker system prune -af --volumes 2>/dev/null || true
log_success "System prune completed."
# Step 9: Clear buildx cache (if available)
log "\n${YELLOW}>> Step 9: Clearing buildx cache...${NC}"
docker buildx prune -af 2>/dev/null || true
log_success "Buildx cache cleared."
# Step 10: Final verification
log "\n${YELLOW}>> Step 10: Final verification...${NC}"
CONTAINERS_REMAINING=$(docker ps -aq 2>/dev/null | wc -l | tr -d ' ')
IMAGES_REMAINING=$(docker images -q 2>/dev/null | wc -l | tr -d ' ')
VOLUMES_REMAINING=$(docker volume ls -q 2>/dev/null | wc -l | tr -d ' ')
BUILD_CACHE_SIZE=$(docker system df --format '{{.BuildCache}}' 2>/dev/null | head -1 || echo "0B")
log_info "Remaining containers: $CONTAINERS_REMAINING"
log_info "Remaining images: $IMAGES_REMAINING"
log_info "Remaining volumes: $VOLUMES_REMAINING"
log_info "Build cache: $BUILD_CACHE_SIZE"
log "\n${GREEN}======================================================"
log "${GREEN}Docker cleanup complete! Your Docker is now clean. 🚀"
log "${GREEN}======================================================${NC}"
exit 0
@fabiobarboza7
Copy link
Author

Example:

Screenshot 2025-11-05 at 13 14 44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment