From 2b2c7572b21a7b954faef1aa0cce5a62b237fb8e Mon Sep 17 00:00:00 2001 From: Sebastian <379651+czarly@users.noreply.github.com> Date: Sun, 24 Mar 2024 15:04:05 +0100 Subject: [PATCH] delete what's not needed --- cleanup-volumes.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 cleanup-volumes.sh diff --git a/cleanup-volumes.sh b/cleanup-volumes.sh new file mode 100755 index 00000000..47f1c25b --- /dev/null +++ b/cleanup-volumes.sh @@ -0,0 +1,42 @@ +BASEPATH="$(dirname "$0")" +source $BASEPATH/.env + +IFS=':' read -ra parts <<< $COMPOSE_FILE + +used_volumes=() + +for part in "${parts[@]}"; do + + # Convert YAML to JSON using yaml2json + json=$(yaml2json "$BASEPATH/$part") + + # Extract volumes using jq + volumes=$(echo "$json" | jq -r '.volumes | keys[]' 2> /dev/null) + + # Convert volumes to an array + prefix="rpc_" + IFS=$'\n' read -r -d '' -a volumes_array <<< "$(printf "%s\n" "${volumes[@]}" | sed "/^$/! s/^/$prefix/")" + + used_volumes=("${used_volumes[@]}" "${volumes_array[@]}") +done + +on_disk=($(docker volume ls --format '{{.Name}}' | grep '^rpc_')) + +unused_volumes=() + +for element in "${on_disk[@]}"; do + # Check if the element exists in array2 + if [[ ! "${used_volumes[@]}" =~ "$element" ]]; then + # If not, add it to the difference array + unused_volumes+=("$element") + fi +done + +if [ "$1" = "--remove-from-disk" ]; then + # Iterate over volumes in the difference array and remove them from disk + for volume in "${unused_volume[@]}"; do + docker volume rm "$volume" + done +else + printf '%s\n' "${unused_volumes[@]}" +fi