Skip to content

Instantly share code, notes, and snippets.

@vps-victor
Last active February 11, 2024 02:05
Show Gist options
  • Select an option

  • Save vps-victor/d6e4d83806bd008e4d20d36d728ad9c0 to your computer and use it in GitHub Desktop.

Select an option

Save vps-victor/d6e4d83806bd008e4d20d36d728ad9c0 to your computer and use it in GitHub Desktop.
Alternative Docker PRUNE command for older Docker API versions.
#!/bin/bash
# Alternative Docker PRUNE command for older Docker API versions.
# Colored & Bolded version
# Color codes
BOLD=$(tput bold)
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
RESET=$(tput sgr0)
# STEP 1 - CONDITION TO REMOVE CONTAINERS
read -p "${BOLD}${GREEN}Step 1 - Would you like to remove containers? (y/n): ${RESET}" remove_containers
if [ "$remove_containers" == "y" ]; then
# Remove all Docker containers
if [ "$(sudo docker ps -aq)" ]; then
sudo docker rm -f $(sudo docker ps -aq)
echo "${BOLD}All containers have been removed successfully.${RESET}"
else
echo "${BOLD}All clear, there were no running containers.${RESET}"
fi
else
echo "${BOLD}Skipping container removal.${RESET}"
fi
# STEP 2 - CONDITION TO REMOVE VOLUMES
read -p "${BOLD}${GREEN}Step 2 - Would you like to remove volumes? (y/n): ${RESET}" remove_volumes
if [ "$remove_volumes" == "y" ]; then
# Remove all Docker volumes
if [ "$(sudo docker volume ls -q)" ]; then
sudo docker volume rm $(sudo docker volume ls -q)
echo "${BOLD}All volumes have been removed successfully.${RESET}"
else
echo "${BOLD}All clear, there were no Docker volumes.${RESET}"
fi
else
echo "${BOLD}Skipping volume removal.${RESET}"
fi
# STEP 3 - CONDITION TO REMOVE IMAGES
read -p "${BOLD}${GREEN}Step 3 - Would you like to remove images? (y/n): ${RESET}" remove_images
if [ "$remove_images" == "y" ]; then
# Remove all Docker images
if [ "$(sudo docker images -q)" ]; then
sudo docker rmi -f $(sudo docker images -q)
echo "${BOLD}All images have been removed successfully.${RESET}"
else
echo "${BOLD}All clear, there were no Docker images.${RESET}"
fi
else
echo "${BOLD}Skipping image removal.${RESET}"
fi
#!/bin/bash
# Alternative Docker PRUNE command for older Docker API versions.
# Raw text colors
# STEP 1 - CONDITION TO REMOVE CONTAINERS
read -p "Step 1 - Would you like to remove containers? (y/n): " remove_containers
if [ "$remove_containers" == "y" ]; then
# Remove all Docker containers
if [ "$(sudo docker ps -aq)" ]; then
sudo docker rm -f $(sudo docker ps -aq)
echo "All containers have been removed successfully."
else
echo "All clear, there were no running containers."
fi
else
echo "Skipping container removal."
fi
# STEP 2 - CONDITION TO REMOVE VOLUMES
read -p "Step 2 - Would you like to remove volumes? (y/n): " remove_volumes
if [ "$remove_volumes" == "y" ]; then
# Remove all Docker volumes
if [ "$(sudo docker volume ls -q)" ]; then
sudo docker volume rm $(sudo docker volume ls -q)
echo "All volumes have been removed successfully."
else
echo "All clear, there were no Docker volumes."
fi
else
echo "Skipping volume removal."
fi
# STEP 3 - CONDITION TO REMOVE IMAGES
read -p "Step 3 - Would you like to remove images? (y/n): " remove_images
if [ "$remove_images" == "y" ]; then
# Remove all Docker images
if [ "$(sudo docker images -q)" ]; then
sudo docker rmi -f $(sudo docker images -q)
echo "All images have been removed successfully."
else
echo "All clear, there were no Docker images."
fi
else
echo "Skipping image removal."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment