Created
May 4, 2020 19:03
-
-
Save pgilad/db4c5017c2a5250d5b9a2076bfed21b3 to your computer and use it in GitHub Desktop.
Docker volume prune with until workaround (filter for --until isn't supported)
This file contains 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
#!/usr/bin/env bash | |
set -euo pipefail | |
volumes=$(docker volume ls --filter dangling=true --quiet) | |
if [[ -z "$volumes" ]]; then | |
echo "No dangling volumes found" | |
exit 0 | |
fi | |
for volume in $volumes; do | |
if [[ -z "$volume" ]]; then | |
continue | |
fi | |
echo "Checking creation date for $volume" | |
created_at=$(docker inspect "$volume" --format '{{.CreatedAt}}') | |
seconds_since_created=$(($(date +%s) - $(date -d"$created_at" +%s))) | |
echo "Created $seconds_since_created ago" | |
if [[ "$seconds_since_created" -gt "3600" ]]; then | |
echo "Deleting volume $volume" | |
docker volume rm "$volume" --force | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment