Skip to content

Instantly share code, notes, and snippets.

@Karreg
Last active October 28, 2020 07:08
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Karreg/84206b9711cbc6d0fbbe77a57f705979 to your computer and use it in GitHub Desktop.
Save Karreg/84206b9711cbc6d0fbbe77a57f705979 to your computer and use it in GitHub Desktop.
Script to cleanup Docker leftovers, related to https://github.com/moby/moby/issues/22207
#!/bin/bash
echo "WARN: This will remove everything from docker: volumes, containers and images. Will you dare? [y/N] "
read choice
if [ \( "$choice" == "y" \) -o \( "$choice" == "Y" \) ]
then
sudo echo "> sudo rights [OK]"
sizea=`sudo du -sh /var/lib/docker/aufs`
echo "Stopping all running containers"
containers=`docker ps -a -q`
if [ -n "$containers" ]
then
docker stop $containers
fi
echo "Removing all docker images and containers"
docker system prune -f
echo "Stopping Docker daemon"
sudo service docker stop
echo "Removing all leftovers in /var/lib/docker (bug #22207)"
sudo rm -rf /var/lib/docker/aufs
sudo rm -rf /var/lib/docker/image/aufs
sudo rm -f /var/lib/docker/linkgraph.db
echo "Starting Docker daemon"
sudo service docker start
sizeb=`sudo du -sh /var/lib/docker/aufs`
echo "Size before full cleanup:"
echo " $sizea"
echo "Size after full cleanup:"
echo " $sizeb"
fi
@TP75
Copy link

TP75 commented Aug 28, 2017

Thank you so much for this excellent script. We are using it now regularily to clean our VMs before installing new docker images. Great help and an almost perfect workaround for the orphaned diffs #22207 issue now. @TP75

@Karreg
Copy link
Author

Karreg commented Sep 6, 2017

You're welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment