Public | Automated Build

Last pushed: a year ago
Short Description
Delete orphaned docker volumes
Full Description

docker-cleanup-volumes





WARNING: Use at your own risk, always test with the --dry-run parameter first. If it's not compatible with your system or Docker version it will delete all your volumes.

Shellscript to delete orphaned docker volumes in /var/lib/docker/volumes and /var/lib/docker/vfs/dir
Docker version 1.4.1 up to 1.10.x

Note about Docker 1.9 and up

To delete orphaned volumes in Docker 1.9 and up you can also use the built-in docker volume commands instead of this docker-cleanup-volumes script. The built-in command also deletes any directory in /var/lib/docker/volumes that is not a volume so make sure you didn't put anything in there you want to save:
List:

$ docker volume ls -qf dangling=true

Cleanup:

$ docker volume rm $(docker volume ls -qf dangling=true)

Or, handling a no-op better but Linux specific:

$ docker volume ls -qf dangling=true | xargs -r docker volume rm

Precautions

  1. Don't use with boot2docker, docker machine or any tools that use a virtual machine to run docker, unless you run the script on the virtual machine itself (meaning don't run this script or docker command from your MAC).
  2. Backup your volumes because.. you know.. the script may not like your system.
  3. When using the script for the first time or after upgrading the host Docker version, run the script with the --dry-run parameter first to make sure it works okay and doesn't delete any volumes that shouldn't be deleted. If you feel bold and run it without --dry-run anyway, make sure you did 1.

Usage standalone script

$ sudo ./docker-cleanup-volumes.sh [--dry-run] [--verbose]

--dry-run : Use the --dry-run option to have the script print the volumes that would have been deleted without actually deleting them.
--verbose : Have the script output more information.

Running from Docker

Run the "latest" forward compatible Docker client version (works with host Docker 1.4.x up to 1.9.x)

$ docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes --dry-run

If you symlinked /var/lib/docker to somewhere else make sure you tell the Docker container where it is by providing the real path or by using readlink in volume parameter.

$ docker run -v /var/run/docker.sock:/var/run/docker.sock -v $(readlink -f /var/lib/docker):/var/lib/docker --rm martin/docker-cleanup-volumes --dry-run

Running from docker using the host docker binary

It is also possible to use the host docker binary by mounting the host docker bin directory. This way you make sure the Docker versions are the same between host and container. For example:

$ docker run -v $(which docker):/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -v $(readlink -f /var/lib/docker):/var/lib/docker --rm martin/docker-cleanup-volumes --dry-run
Docker Pull Command
Owner
martin
Source Repository

Comments (5)
docbill
a year ago

What is the expected output?

This is what I get:

-bash-4.2$ docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes
Unable to find image 'martin/docker-cleanup-volumes:latest' locally
latest: Pulling from martin/docker-cleanup-volumes
e6c44a677827: Pull complete
e665190e5bfd: Pull complete
3f134245a2ac: Pull complete
Digest: sha256:0cae7e8ac0cbe678c9458f736dea727d5439d92c558993421687c5dda0624e9d
Status: Downloaded newer image for martin/docker-cleanup-volumes:latest

Delete unused volume directories from /var/lib/docker/volumes

Directory /var/lib/docker/vfs/dir does not exist or is empty, skipping.

lekkas
2 years ago

Very useful, thanks!

martin
2 years ago

All tags have been updated now.

viatcheslavmogilevsky
2 years ago

Why build for tag 1.4.1 is not updated according pull/13 ?

zaziork
2 years ago

Looks like a very useful tool! Thanks for making this available.
I needed a quick way to remove unattached Docker volumes that were spawned during my dev work, so I wrote a small CLI utility in Java that scans the system to generate simple resource snapshot reports about Docker containers and volumes, and provides the ability to clean up (move and/or remove) unattached volumes if required.
But, I like that your script runs in a Docker container - simpler to implement than mine (no need to have a JRE installed) and far more "docker-ish"!
(FYI mine is: https://www.zaziork.com/dockervolumesmanager and the source is on github: https://github.com/ZWS2014/dockervolumesmanager )