Created
July 16, 2025 15:11
-
-
Save zvchei/ce7144098d90a4d26df040a512001fcc to your computer and use it in GitHub Desktop.
An interactive bash script for cleaning up Docker images and volumes by given search pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Docker Image and Volume Cleanup Script | |
| # | |
| # Description: | |
| # This script helps clean up Docker images and volumes by searching for items | |
| # that match a specified pattern. It provides a safe way to remove Docker | |
| # resources by showing what will be deleted and requiring explicit confirmation. | |
| # | |
| # Usage: ./dclean.sh <search_pattern> | |
| # Example: ./dclean.sh myapp | |
| # | |
| # WARNING: Deletion operations are irreversible! | |
| # Check if argument is provided | |
| if [ $# -eq 0 ]; then | |
| echo "Usage: $0 <search_pattern>" | |
| echo "Example: $0 myapp" | |
| exit 1 | |
| fi | |
| ARG="$1" | |
| echo "=== Docker Image Cleanup ===" | |
| echo "Searching for Docker images matching pattern: '$ARG'" | |
| echo | |
| # Search for images and display results | |
| IMAGES=$(docker image ls | grep "$ARG") | |
| if [ -z "$IMAGES" ]; then | |
| echo "No Docker images found matching pattern: '$ARG'" | |
| else | |
| echo "Found the following Docker images:" | |
| echo "$IMAGES" | |
| echo | |
| # Ask for confirmation | |
| read -p "Do you want to delete these images? This operation is irreversible. Type 'yes' to proceed: " confirmation | |
| if [ "$confirmation" = "yes" ]; then | |
| echo "Deleting Docker images..." | |
| docker image ls | grep "$ARG" | awk '{print $1}' | while read x; do | |
| echo "Removing image: $x" | |
| docker image rm "$x" | |
| done | |
| echo "Docker images cleanup completed." | |
| else | |
| echo "Docker images cleanup cancelled." | |
| exit 0 | |
| fi | |
| fi | |
| echo | |
| echo "=== Docker Volume Cleanup ===" | |
| echo "Searching for Docker volumes matching pattern: '$ARG'" | |
| echo | |
| # Search for volumes and display results | |
| VOLUMES=$(docker volume ls | grep "$ARG") | |
| if [ -z "$VOLUMES" ]; then | |
| echo "No Docker volumes found matching pattern: '$ARG'" | |
| else | |
| echo "Found the following Docker volumes:" | |
| echo "$VOLUMES" | |
| echo | |
| # Ask for confirmation | |
| read -p "Do you want to delete these volumes? This operation is irreversible. Type 'yes' to proceed: " confirmation | |
| if [ "$confirmation" = "yes" ]; then | |
| echo "Deleting Docker volumes..." | |
| docker volume ls | grep "$ARG" | awk '{print $2}' | while read x; do | |
| echo "Removing volume: $x" | |
| docker volume rm "$x" | |
| done | |
| echo "Docker volumes cleanup completed." | |
| else | |
| echo "Docker volumes cleanup cancelled." | |
| fi | |
| fi | |
| echo | |
| echo "Cleanup script finished." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment