Docker Volume - Remove, Prune

To remove the volumes, we can use the rm option.

Listing all the volume that are available right now-

gaurav@learning-ocean:~$ docker volume ls
DRIVER    VOLUME NAME
local     abc
local     mytest
local     mytest1
local     mytest2
local     mytest3
local     mytest4
gaurav@learning-ocean:~$

Now, lets delete mytest, mytest1, mytest2 volumes-

gaurav@learning-ocean:~$ docker volume rm mytest
mytest
gaurav@learning-ocean:~$ docker volume rm mytest1 mytest2
mytest1
mytest2
gaurav@learning-ocean:~$

We have only 3 volumes left now-

gaurav@learning-ocean:~$ docker volume ls
DRIVER    VOLUME NAME
local     abc
local     mytest3
local     mytest4
gaurav@learning-ocean:~$

Now, to remove all the unused volumes, we can use prune command -

gaurav@learning-ocean:~$ docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
mytest3
mytest4
Total reclaimed space: 0B
gaurav@learning-ocean:~$

The above command only deleted mytest3 and mytest4 and not the abc volume because it is still associated with a container.

gaurav@learning-ocean:~$ docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                 NAMES
9fac9587edfd   mysql     "docker-entrypoint.s…"   13 minutes ago   Up 13 minutes   3306/tcp, 33060/tcp   xyz_com_database12
gaurav@learning-ocean:~$ docker volume ls
DRIVER    VOLUME NAME
local     abc

To remove the volume above we first have to remove it then only we will be able to delete the volume-

gaurav@learning-ocean:~$ docker container rm -f 9f
9f

Now we can delete the abc volume as it is in unused state now using prune command -

gaurav@learning-ocean:~$ docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
abc
Total reclaimed space: 205.1MB
gaurav@learning-ocean:~$ docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
gaurav@learning-ocean:~$
gaurav@learning-ocean:~$ docker volume ls
DRIVER    VOLUME NAME
gaurav@learning-ocean:~$