cd /news/developer-tools/docker-orphaned-overlay2-cleanup-scr… · home topics developer-tools article
[ARTICLE · art-10192] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

docker orphaned overlay2 cleanup script

This article presents a Bash script called `docker-overlay-prune.sh` that identifies and removes orphaned overlay2 directories in `/var/lib/docker/overlay2/` which are no longer associated with any Docker image or mounted filesystem. The script runs with dry-run mode by default, only listing unused directories, but includes a `-n` flag to enable actual deletion of those directories.

read2 min views25 publishedApr 16, 2025
docker-overlay-prune.sh

      This file contains hidden or 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

Show hidden characters

#!/bin/bash

[[ $(id -u) -eq 0 ]] || exec sudo /bin/bash -c "$(printf '%q ' "$BASH_SOURCE" "$@")"

progname=$(basename $0)

quiet=false

no_dry_run=false

while getopts ":qn" opt; do

  case "$opt" in

    q)

      quiet=true

      ;;

    n)

      no_dry_run=true

      ;;

    ?)

      echo "unexpected option ${opt}"

      echo "usage: ${progname} [-q|--quiet]"

      echo "    -q: no output"

      echo "    -n: no dry run (will remove unused directories)"

      exit 1

      ;;

esac

done

shift "$(($OPTIND -1))"

[[ ${quiet} = false ]] || exec /bin/bash -c "$(printf '%q ' "$BASH_SOURCE" "$@")" > /dev/null

images=( $(docker image ls --all --format "{{.ID}}") )

mounted=$(mount | grep "/var/lib/docker/overlay2/")

dirs=( $(find /var/lib/docker/overlay2 -mindepth 1 -maxdepth 1 -type d -not -path ./l -exec basename {} \;) )

unused=()

echo "Scanning for used overlays2 dirs."

for d in "${dirs[@]}"; do used=0

  dname=${d//"-init"/}


  echo ${mounted} | grep -q ${dname}

  if [[ $? = 0 ]]; then

    continue

fi

  for id in ${images[@]}; do

    docker inspect $id | grep -q $dname

    if [[ $? = 0 ]]; then

      used=1

      break

    fi

done

  if [[ ${used} -eq 0 ]]; then

    unused+=("${d}")

fi

done

if [ ${#unused[@]} -gt 0 ]; then

  [[ ${no_dry_run} = true ]] || echo "Could remove:  (to automatically remove, use the -n, "'"'"no-dry-run"'"'" flag)"

  for d in "${unused[@]}"; do

    if [[ ${no_dry_run} = true ]]; then

      echo "Removing /var/lib/docker/overlay2/${d}"

      rm -rf /var/lib/docker/overlay2/${d}

    else

      echo " /var/lib/docker/overlay2/${d}"

    fi

done

echo Done

else

echo "All overlay2 directories are used, nothing to clean up."

fi

── more in #developer-tools 4 stories · sorted by recency
── more on @docker 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/docker-orphaned-over…] indexed:0 read:2min 2025-04-16 ·