DEV Community

Sardorbek Imomaliev
Sardorbek Imomaliev

Posted on • Edited on

TIL: Docker | Prune Only Unnamed Volumes

Story

I was trying to do some cleanup of space for docker and needed to prune my unnamed volumes names.

Question

How to prune all unnamed docker volumes?

Answer

Use --filter option. From docs:

By default, all unused volumes are removed. You can limit the scope using
the --filter flag. For instance, the following command only removes
volumes which are not labelled with the keep label:

$ docker volume prune --filter "label!=keep"

But to create correct filter first we need to inspect volumes to find what labels and other parameters are present for them

$ docker volume inspect project_volume
[
    {
        "CreatedAt": "2021-09-30T11:35:55Z",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "project",
            "com.docker.compose.version": "2.0.0",
            "com.docker.compose.volume": "project_volume"
        },
        "Mountpoint": "/var/lib/docker/volumes/project_project_data/_data",
        "Name": "project_project_data",
        "Options": null,
        "Scope": "local"
    }
]
Enter fullscreen mode Exit fullscreen mode

So we could filter our volumes like this

$ docker volume prune --filter 'label!=com.docker.compose.project'
Enter fullscreen mode Exit fullscreen mode

Note negative filtering does not work for docker volume ls for some reason.

$ docker volume ls --filter 'label!=com.docker.compose.project'
Error response from daemon: Invalid filter 'label!'
Enter fullscreen mode Exit fullscreen mode

Links

Top comments (0)