Created
February 17, 2026 12:49
-
-
Save zvchei/4694a7c1b5bcbb9f8d254d724cb1ec72 to your computer and use it in GitHub Desktop.
Delete Docker images and volumes that match a specific 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: ./doclean.sh <search_pattern> | |
| # Example: ./doclean.sh myapp | |
| # | |
| # WARNING: Deletion operations are irreversible! | |
| # This script is created with the help of GPT-5 using multiple iterations and | |
| # manual refactoring. Use it at your own risk. --zvchei | |
| # MIT-0 License | |
| # Copyright (c) 2025 | |
| # | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| # of this software and associated documentation files (the "Software"), to deal | |
| # in the Software without restriction, including without limitation the rights | |
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| # copies of the Software, and to permit persons to whom the Software is | |
| # furnished to do so. | |
| # | |
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| # SOFTWARE. | |
| # 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 Container Stop ===" | |
| echo "Searching for running containers matching pattern: '$ARG'" | |
| echo | |
| # List running containers with names matching ARG (ignore grep exit code) | |
| RUNNING_CONTAINERS=$(docker ps --format '{{.ID}} {{.Names}}' | grep "$ARG" || true) | |
| if [ -z "$RUNNING_CONTAINERS" ]; then | |
| echo "No running containers found matching pattern: '$ARG'" | |
| else | |
| echo "Found the following running containers:" | |
| echo "$RUNNING_CONTAINERS" | |
| echo | |
| read -p "Do you want to stop these containers? Type 'yes' to proceed: " confirmation | |
| if [ "$confirmation" = "yes" ]; then | |
| echo "Stopping matching containers..." | |
| docker ps --format '{{.ID}} {{.Names}}' | grep "$ARG" | awk '{print $1}' | while read cid; do | |
| echo "Stopping container: $cid" | |
| docker stop "$cid" | |
| done | |
| echo "Container stop completed." | |
| else | |
| echo "Container stop cancelled." | |
| fi | |
| fi | |
| echo | |
| echo "=== Docker System Prune ===" | |
| echo "Performing 'docker system prune -f' to clean up unused resources..." | |
| echo | |
| docker system prune -f | |
| echo | |
| echo "=== Docker Image Cleanup ===" | |
| echo "Searching for Docker images matching pattern: '$ARG'" | |
| echo | |
| # Search for images and display results | |
| IMAGES=$(docker image ls --format table | 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 --format table | 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 --format table | 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. DATA LOSS IMMINENT! Type 'YES' to proceed: " confirmation | |
| if [ "$confirmation" = "YES" ]; then | |
| echo "Deleting Docker volumes..." | |
| docker volume ls --format table | 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