Instantly share code, notes, and snippets.

What would you like to do?
Script to cleanup Docker leftovers, related to
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" \) ]
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" ]
docker stop $containers
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"

This comment has been minimized.

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


This comment has been minimized.


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