diff --git a/commands/clean-docker-volume/README.md b/commands/clean-docker-volume/README.md new file mode 100644 index 0000000..4a1a453 --- /dev/null +++ b/commands/clean-docker-volume/README.md @@ -0,0 +1,32 @@ +# ๐Ÿงน dock-volume-cleanup + +A handy script to clean up unused Docker volumes. + +--- + +## ๐Ÿ”ง Features + +- ๐Ÿ—‘๏ธ Removes **dangling volumes** (default behavior) +- ๐Ÿ’ฃ Optional: Use `--force-all` to remove **all unused volumes**, even named ones +- ๐Ÿ“‹ Lists remaining volumes after cleanup +- โœ… Safe: does **not** touch containers or images + +--- + +## ๐Ÿ“ฆ Requirements + +- Docker must be **installed** and **running** + +--- + +## ๐Ÿš€ Installation + +1. Save the script as `docker-volume-cleanup.sh` +2. Make it executable: + ```bash + chmod +x docker-volume-cleanup.sh + ``` + ![Screenshot from 2025-06-18 08-32-10](https://github.com/user-attachments/assets/fa33b51c-85f6-4ac8-aca2-3485b592f989) + + + ![image](https://github.com/user-attachments/assets/3af6a851-0639-4c92-b76e-b8e305562a50) diff --git a/commands/clean-docker-volume/docker-volume-cleanup.sh b/commands/clean-docker-volume/docker-volume-cleanup.sh new file mode 100644 index 0000000..a593118 --- /dev/null +++ b/commands/clean-docker-volume/docker-volume-cleanup.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +if [ "$EUID" -ne 0 ]; then + echo "Please run as root or use sudo" + exit 1 +fi + +command_name="dock-volume-cleanup" +FORCE_ALL="$1" + +cleanup_volumes() { + echo "Cleaning up dangling Docker volumes..." + docker volume prune -f + + if [ "$FORCE_ALL" == "--force-all" ]; then + echo "Forcefully removing all unused Docker volumes (including named ones)..." + # Get all volumes not in use and remove them + docker volume ls -q | while read volume; do + # Check if the volume is used by any container + usage=$(docker ps -a --filter volume="$volume" -q) + if [ -z "$usage" ]; then + echo "Removing unused volume: $volume" + docker volume rm "$volume" + fi + done + fi + + echo "Listing remaining Docker volumes..." + docker volume ls + echo "Docker volume cleanup complete!" +} + +if ! command -v $command_name &> /dev/null; then + echo "Installing $command_name command..." + + script_path=$(realpath "$0") + cp "$script_path" /usr/local/bin/$command_name + chmod +x /usr/local/bin/$command_name + + echo "$command_name command installed successfully! You can now use it by typing '$command_name'." + exit 0 +else + echo "$command_name is already installed, running cleanup..." + cleanup_volumes +fi