Skip to content

docker-volume-clean added #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions commands/clean-docker-volume/README.md
Original file line number Diff line number Diff line change
@@ -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)
45 changes: 45 additions & 0 deletions commands/clean-docker-volume/docker-volume-cleanup.sh
Original file line number Diff line number Diff line change
@@ -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