332

EDIT (2/19/21): A lot of time has elapsed since I asked this original question years ago and I've seen a flurry of activity since then. I re-selected an answer which I think is consistent with the most localized and safe option for solving this issue (which is typically associated with docker-compose). While docker did introduce the prune command, it is generally a dangerous operation and I would be cautious about using it as you may unintentionally impact other applications or setups you have on your machine


I've been having issues with removing Docker volumes with Docker 1.9.1.

I've removed all my stopped containers so that docker ps -a returns empty.

When I use docker volume ls, I'm given a whole host of Docker containers:

docker volume ls
DRIVER              VOLUME NAME
local               a94211ea91d66142886d72ec476ece477bb5d2e7e52a5d73b2f2f98f6efa6e66
local               4f673316d690ca2d41abbdc9bf980c7a3f8d67242d76562bbd44079f5f438317
local               eb6ab93effc4b90a2162e6fab6eeeb65bd0e4bd8a9290e1bad503d2a47aa8a78
local               91acb0f7644aec16d23a70f63f70027899017a884dab1f33ac8c4cf0dabe5f2c
local               4932e2fbad8f7e6246af96208d45a266eae11329f1adf176955f80ca2e874f69
local               68fd38fc78a8f02364a94934e9dd3b5d10e51de5b2546e7497eb21d6a1e7b750
local               7043a9642614dd6e9ca013cdf662451d2b3df6b1dddff97211a65ccf9f4c6d47
#etc x 50

Since none of these volumes contain anything important, I try to purge all the volumes with docker volume rm $(docker volume ls -q).

In the process, the majority are removed, but I get back:

Error response from daemon: Conflict: volume is in use
Error response from daemon: Conflict: volume is in use
Error response from daemon: Conflict: volume is in use
Error response from daemon: Conflict: volume is in use
Error response from daemon: Conflict: volume is in use

For a sizeable portion of them. If I don't have any containers existing in the first place, how are these volumes being used?

Tkwon123
  • 3,672
  • 2
  • 13
  • 13
  • 12
    docker uses reference counting to check if a volume is still in use; this is all done in-memory; this may be a bug or a race condition somehow, which resulted in the container being removed, but the counter not being updated. A restart of the daemon should resolve this, but, yes it's possible there's a bug somewhere. Is there something special in your setup (e.g. Are you using docker-in-docker, Swarm?). Do you use some script or tool to cleanup your containers? – thaJeztah Jan 07 '16 at 23:32
  • 1
    In docker `1.9.0`. I try your steps, and can remove all the volumes. – firelyu Jan 08 '16 at 04:00
  • 8
    Hey thanks @thaJeztah restarting the Docker daemon (`sudo service docker stop` and `sudo service docker start`) cleared out all of these ghost volumes for me. Moreover, it seems like I am now able to remove volumes without issue using the docker rm -v command. Only notable differences in usage is that I've been using docker-compose on Ubuntu 15.10. I'll report back if I'm ever able to replicate this problem but otherwise it seems like a simple restart will suffice. Thanks! – Tkwon123 Jan 08 '16 at 13:17
  • 4
    even after reboot it still says docker volume is in use.. – holms Jul 05 '16 at 01:31
  • 15
    If you use docker compose, you can add -v to the down command, to remove the volumes. – Niels Bech Nielsen Dec 01 '16 at 10:31
  • docker caches some information abount containers etc. on disk. perhaps there are some left overs which are confusing the system. have you tried cleaning dangling images etc. which might cause an issue? docker rmi $(docker images -f "dangling=true" -q) – user7296055 Jan 03 '17 at 14:42
  • 6
    I fixed this by stopping docker then removing the volumes from the file system and starting docker again. `service docker stop && rm -rf /var/lib/docker/volumes/TheVolumIdYouWantToRemove && service docker start` – jfgrissom Nov 23 '17 at 19:09
  • 2
    I'm on mac and none of the solutions proposed here work – Thomas Oct 04 '18 at 17:13

12 Answers12

484

Perhaps the volume was created via docker-compose? If so, it should get removed by:

docker-compose down --volumes

Credit to Niels Bech Nielsen!

Robert K. Bell
  • 9,350
  • 2
  • 35
  • 50
  • 10
    This works :) It's good to note that this also removes all the containers themselves. This can be unwanted if you changed files in the container that are not on a permanent mount and not in the image. – Alexander Varwijk Jun 11 '19 at 12:52
  • 2
    You just saved my day. None of the force or other optiosn were working. Was it cos this was a config created by compose and there is some conflict between compose and docker reading those mouts – Shashikant Soni Nov 06 '20 at 08:35
  • 7
    anyway to only remove a single volume and not all volumes? – David 天宇 Wong Feb 16 '21 at 23:25
  • 3
    Just make sure, that you are in the same folder where the `docker-compose.yaml` lives. Otherwise it will not find the volume names. But works - Thanks! – gies0r Feb 27 '21 at 13:13
  • 2
    If you changed the name of the volumes in your compose files, this may result in orphaned containers. There is a warning printed out in the console by docker, and you can add the ` --remove-orphans` flag to clean then you can rm the associated volumes – Cyril Duchon-Doris Jul 19 '21 at 12:53
  • Generally works for me, for some weird reason this did not work. But I believe it's because I used a `docker compose run` command which started an independent container that used the volume. Went into the docker GUI and found it lingering in the containers and delete, then this worked. – Tom Mertz Mar 25 '22 at 18:43
  • 2
    BIG FAT WARNING. This will delete ALL your images in the docker-compose so make sure this is something you want t odo – Ross W Aug 14 '22 at 17:55
  • I think what @RossW means is the following: it will delete your Volumes!!! So if you have a database loaded into your volume be sure you really want to do this! For me the issue still stands. I want to be able to stop my containers and stop the volumes from being used without deleting them. – Jan Dries Apr 26 '23 at 07:46
257

You can use these functions to brutally remove everything Docker related:

removecontainers() {
    docker stop $(docker ps -aq)
    docker rm $(docker ps -aq)
}

armageddon() {
    removecontainers
    docker network prune -f
    docker rmi -f $(docker images --filter dangling=true -qa)
    docker volume rm $(docker volume ls --filter dangling=true -q)
    docker rmi -f $(docker images -qa)
}

You can add those to your ~/Xrc file, where X is your shell interpreter (~/.bashrc if you're using bash) file and reload them via executing source ~/Xrc. Also, you can just copy paste them to the console and afterwards (regardless the option you took before to get the functions ready) just run:

armageddon

It's also useful for just general Docker clean up. Have in mind that this will also remove your images, not only your containers (either running or not) and your volumes of any kind.

Marcos Aguayo
  • 6,840
  • 8
  • 28
  • 61
David González Ruiz
  • 3,205
  • 1
  • 13
  • 11
252

Volume can be in use by one of stopped containers. You can remove such containers by command:

docker container prune

then you can remove not used volumes

docker volume prune
Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88
69

I am fairly new to Docker. I was cleaning up some initial testing mess and was not able to remove a volume either. I had stopped all the running instances, performed a docker rmi -f $(docker image ls -q), but still received the Error response from daemon: unable to remove volume: remove uuid: volume is in use.

I did a docker system prune and it cleaned up what was needed to remove the last volume:

[0]$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
... about 15 containers UUID's truncated

Total reclaimed space: 2.273MB
[0]$ docker volume ls
DRIVER              VOLUME NAME
local              uuid
[0]$ docker volume rm uuid
uuid
[0]$

docker system prune

The client and daemon API must both be at least 1.25 to use this command. Use the docker version command on the client to check your client and daemon API versions.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Benjamin West
  • 839
  • 6
  • 6
22

As long as volumes are associated with a container (either running or not), they cannot be removed.

You have to run

docker inspect <container-id>/<container-name>

on each of the running/non-running containers where this volume might have been mounted onto.

If the volume is mounted onto any one of the containers, you should see it in the Mounts section of the inspect command output. Something like this :-

"Mounts": [
            {
                "Type": "volume",
                "Name": "user1",
                "Source": "/var/lib/docker/volumes/user1/_data",
                "Destination": "/opt",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

After figuring out the responsible container(s), use :-

docker rm -f container-1 container-2 ...container-n in case of running containers

docker rm container-1 container-2 ...container-n in case of non-running containers

to completely remove the containers from the host machine.

Then try removing the volume using the command :-

docker volume remove <volume-name/volume-id>

pdoherty926
  • 9,895
  • 4
  • 37
  • 68
16
  1. First prune the unwanted containers with the command:

    docker container prune
    

    (Make sure that you really want to remove your all containers)

  2. After removing all the unwanted containers prune the volume as well using:

    docker volume prune
    
cigien
  • 57,834
  • 11
  • 73
  • 112
Ved pal
  • 161
  • 1
  • 2
12

Currently you can use what docker offers now for a general and more complete cleaning:

docker system prune

To additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag to the command:

docker system prune -a
shackra
  • 277
  • 3
  • 16
  • 56
5

You must remove the container that uses that volume first. List all containers by name, even the existed ones:

docker ps --all --format '{{.Names}}'  

Remove a container:

docker rm NAME

After you removed the container you may remove the volume as well. List the volumes:

docker volume ls

Remove the volume:

docker volume remove VOLUME_NAME
Bar Horing
  • 5,337
  • 3
  • 30
  • 26
  • One reason why this sometimes works after 'docker-compose --volumes' doesn't: because an accidental transient container was made via 'docker compose run' – tjb Nov 15 '22 at 06:26
  • nice, but I find even more useful just to run ´docker ps --all' – xCovelus Aug 24 '23 at 07:49
4

A one liner to give you just the needed details:

docker inspect `docker ps -aq` | jq '.[] | {Name: .Name, Mounts: .Mounts}' | less

search for the volume of complaint, you have the container name as well.

Case Larsen
  • 186
  • 2
  • 5
  • What if there are no containers i.e. "docker ps -aq" does not return anything. The how do we find the mount path – Manoj Kumar May 04 '21 at 06:00
  • `docker volume inspect $(docker volume ls -q) | jq '.[] | {Name: .Name, Mountpoint: .Mountpoint}'` will give you the paths of volumes regardless of whether they are in use by a container... – Case Larsen Jul 14 '21 at 20:06
3

I am pretty sure that those volumes are actually mounted on your system. Look in /proc/mounts and you will see them there. You will likely need to sudo umount <path> or sudo umount -f -n <path>. You should be able to get the mounted path either in /proc/mounts or through docker volume inspect

Jiri Klouda
  • 1,362
  • 12
  • 25
1

You can delete it in your Docker Desktop Application. I have already done that and everything is okay. enter image description here Here:)

UZG1
  • 69
  • 3
  • 8
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 22 '22 at 11:52
  • OP's problem is that they have volumes which are in use while the containers are no longer running. You can not delete a volume that is in use. – Jan Dries Apr 26 '23 at 07:50
-4

You should type this command with flag -f (force):

sudo docker volume rm -f <VOLUME NAME>

Julia
  • 95
  • 1
  • 7