Cleaning a Docker volume with a bash script
Introduction
In this article you can study the script that is used on
Ubuntu Linux
to free up the space taken by Kafka in
a Docker container
.
You can read about how to find the files that take up a lot of space in the article
«Free disk space in Linux»
This script assumes that the VOLUME variable is set to a value that corresponds to the real volume.
Example
Once VOLUME is specified, you need to decide which files are to be deleted. This example deletes files with the extensions log, index, timeindex.
Kafka topics are listed in the array topics
#!/bin/bash # Should be run with sudo: # sudo free_docker_volume_space.sh VOLUME=9f58ba6e027622d11fdc5a67abee3bwc530bf412e1001dd14af7c5ad98ba7c4c EXTENSIONS=(log index timeindex) declare -a topics=("topic1" "topic2" ) for topic in "${topics[@]}"; do echo "Removing ${topic} related logs and indexes" TOPIC_PATH="/var/lib/docker/volumes/${VOLUME}/_data/${topic}" cd $TOPIC_PATH for file in ./*; do EXT="${file##*.}" if [[ " ${EXTENSIONS[*]} " =~ " ${EXT} " ]]; then echo "Removing ${FILENAME}"; rm $file fi done done
Article author: Andrei Olegovich