Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Docker volume prune with until workaround (filter for --until isn't supported)
#!/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