Skip to content

Instantly share code, notes, and snippets.

@aduzsardi
Last active January 19, 2021 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aduzsardi/6dfd0afa81ed3dc112783d5caa017c17 to your computer and use it in GitHub Desktop.
Save aduzsardi/6dfd0afa81ed3dc112783d5caa017c17 to your computer and use it in GitHub Desktop.
Remove docker volumes that have not been used for a time
#!/bin/bash -e
### Author: Alex Duzsardi
### Twitter: @aduzsardi
### GitHub: @aduzsardi
### Keybase: @aduzsardi
### Requirements: bash, date, docker, jq
# Remove docker volumes last used more than a month ago
# https://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html
__older_than_unix_timestamp=$(date +%s --date="1 month ago")
for __volume in $(docker volume ls --format '{{.Name}}'); do
__volume_last_used_date=$(docker volume inspect "$__volume" | jq -r .[].CreatedAt)
__volume_unix_timestamp=$(date +%s --date=$__volume_last_used_date)
if [[ $__volume_unix_timestamp -lt $__older_than_unix_timestamp ]]; then
docker volume rm -f "$__volume"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment