Skip to content

Instantly share code, notes, and snippets.

@broisnischal
Created August 11, 2025 04:51
Show Gist options
  • Select an option

  • Save broisnischal/4e8c5a7430e7a47eb7d5855762f471da to your computer and use it in GitHub Desktop.

Select an option

Save broisnischal/4e8c5a7430e7a47eb7d5855762f471da to your computer and use it in GitHub Desktop.
docker-script
#!/usr/bin/env bash
set -euo pipefail
VERSION="1.1.0"
SCRIPT_NAME="docker-clean"
SCRIPT_URL="https://gist.githubusercontent.com/broisnischal/4e8c5a7430e7a47eb7d5855762f471da/raw/docker-clean.sh"
# Colors
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
BLUE="\033[0;34m"
NC="\033[0m"
skip_prompt=false
dry_run=false
show_help() {
cat <<EOF
${SCRIPT_NAME} - Remove Docker resources safely
Usage: ${SCRIPT_NAME} [OPTIONS]
Options:
-y, --yes Skip confirmation prompts
-h, --help Show this help message
-v, --version Show version
--update Update to latest version
--dry-run Show what will be removed without actually removing
-f, --force Force skip prompts & proceed
Examples:
${SCRIPT_NAME} -y
${SCRIPT_NAME} --dry-run
EOF
exit 0
}
show_version() {
echo "${SCRIPT_NAME} v${VERSION}"
exit 0
}
update_script() {
echo -e "${BLUE}Updating ${SCRIPT_NAME}...${NC}"
curl -sL "$SCRIPT_URL" -o "$0"
chmod +x "$0"
echo -e "${GREEN}Update complete.${NC}"
exit 0
}
confirm() {
local message="$1"
if [ "$skip_prompt" = true ]; then
return 0
fi
read -p "$message [y/N]: " response
case "$response" in
[yY][eE][sS]|[yY]) return 0 ;;
*) return 1 ;;
esac
}
docker_action() {
local description="$1"
local cmd="$2"
if $dry_run; then
echo -e "${YELLOW}[Dry Run]${NC} $description: $cmd"
else
eval "$cmd"
fi
}
# Parse args
for arg in "$@"; do
case $arg in
-y|--yes) skip_prompt=true ;;
-f|--force) skip_prompt=true ;;
--dry-run) dry_run=true ;;
-h|--help) show_help ;;
-v|--version) show_version ;;
--update) update_script ;;
*) echo "Unknown option: $arg"; show_help ;;
esac
done
echo -e "${BLUE}=== Docker Cleanup Tool v${VERSION} ===${NC}"
# Stop all running containers
if confirm "Stop all running containers?"; then
docker_action "Stopping containers" "docker container stop \$(docker container ls -q) 2>/dev/null || true"
fi
# Remove all containers
if confirm "Remove all containers?"; then
docker_action "Removing containers" "docker container rm -f \$(docker container ls -a -q) 2>/dev/null || true"
fi
# Remove all images
if confirm "Remove all images?"; then
docker_action "Removing images" "docker image rm -f \$(docker image ls -a -q) 2>/dev/null || true"
fi
# Remove all volumes
if confirm "Remove all volumes?"; then
docker_action "Removing volumes" "docker volume rm -f \$(docker volume ls -q) 2>/dev/null || true"
fi
# Remove all custom networks
if confirm "Remove all custom networks?"; then
docker_action "Removing networks" "docker network rm \$(docker network ls -q | grep -vE 'bridge|host|none') 2>/dev/null || true"
fi
# Prune everything
if confirm "Prune system (remove unused data)?"; then
docker_action "Pruning system" "docker system prune -a -f --volumes"
fi
echo -e "${GREEN}Docker cleanup completed!${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment