Skip to content

Instantly share code, notes, and snippets.

@pgilad
Created May 4, 2020 19:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgilad/db4c5017c2a5250d5b9a2076bfed21b3 to your computer and use it in GitHub Desktop.
Save pgilad/db4c5017c2a5250d5b9a2076bfed21b3 to your computer and use it in GitHub Desktop.
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