when we use GitLab runner, the runner uses gitlab-runner-helper to create a volume to store data.
after a while when we try to get docker volumes we see many gitlab-runner-helper volumes with names like "runner-wie4xt7--project-3-concurrent-0-cache-*"
To delete this volume I try to create a bash script file to remove volumes whose lifetime exceeds 24 hours
#!/bin/bash
# Define the pattern for volume names to be removed
VOLUME_PATTERN="runner-wie4xt7--project-3-concurrent-0-cache-*"
# Define the maximum age of volumes in hours
MAX_AGE_HOURS=24
# Find and remove volumes matching the pattern and older than the specified age
docker volume ls -q -f name=$VOLUME_PATTERN | while read -r volume; do
creation_time=$(docker volume inspect --format '{{.CreatedAt}}' "$volume")
creation_timestamp=$(date -d "$creation_time" +%s)
current_timestamp=$(date +%s)
age_hours=$(( (current_timestamp - creation_timestamp) / 3600 ))
if [ "$age_hours" -gt "$MAX_AGE_HOURS" ]; then
echo "Removing volume: $volume (Age: $age_hours hours)"
docker volume rm "$volume"
fi
done
is there any way for docker or Gitlab runner to delete this volume automatically?