Last active
January 19, 2021 10:32
-
-
Save aduzsardi/6dfd0afa81ed3dc112783d5caa017c17 to your computer and use it in GitHub Desktop.
Remove docker volumes that have not been used for a time
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
#!/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