How to Remove Orphaned Volumes in Docker

John John (304)
3 minutes

When using Docker sometimes it's necessary to do some cleanup in order to free up space. I ran into an issue recently where I could not start my database container. The error message was:

db_1     | creating subdirectories ... initdb: could not create directory "/var/lib/postgresql/data/global": No space left on device

One simple way to free up space is to delete dangling or orphaned volumes. A dangling volume is a volume that is not referenced by any container.

Posted in these interests:
h/docker12 guides

Let's break this down.

First, if you want to see a list of the dangling volumes you can simply run:

docker volume ls -qf dangling=true

docker volume ls lists the volumes and -qf means list only the ids and filter on dangling=true.

To delete these volumes we'll pass them in to the docker volume rm function which takes a volume id or list of ids. The final command is:

docker volume rm $(docker volume ls -qf dangling=true)
John John (304)
2 minutes

Vim is a powerful tool. I often paste raw data into Vim in order to format it in a specific way, and sometimes I need to clean up the file by removing many blank lines.