133

If I have a docker-compose file like:

version: "3"
services:
  postgres:
    image: postgres:9.4
    volumes:
      - db-data:/var/lib/db
volumes:
  db-data:

... then doing docker-compose up creates a named volume for db-data. Is there a way to remove this volume via docker-compose? If it were an anonymous volume, then docker-compose rm -v postgres would do the trick. But as it stands, I don't know how to remove the db-data volume without reverting to docker commands. It feels like this should be possible from within the docker-compose CLI. Am I missing something?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Bosh
  • 8,138
  • 11
  • 51
  • 77

4 Answers4

168
docker-compose down -v

removes all volumes attached. See the docs

Soviut
  • 88,194
  • 49
  • 192
  • 260
herm
  • 14,613
  • 7
  • 41
  • 62
  • 7
    You can also use `docker-compose rm -f -v` – Tarun Lalwani Aug 06 '17 at 11:32
  • 23
    @herm Thanks, works. Comment above mine does **not** work for **named** volumes as clearly stated in the docs. Btw., I find it extremely confusing that identically named options (`-v`) behave entirely different depending on the command (`rm` vs `down`). – qqilihq Nov 08 '17 at 17:01
  • 4
    Although not `docker-compose` per se, you can also remove a volume with `docker volume rm [name]` e.g `docker volume rm db-data`. See also `docker volume prune` – David Thomas Dec 18 '17 at 06:53
  • 1
    This only removes anonymously created volumes, not the named ones. – Mike Doe Dec 07 '18 at 12:21
  • If you do not want to duplicate configuration data, you have to parse docker-compose.yaml and execute `docker volume rm` based on the volumes found – rhoerbe Dec 18 '18 at 08:12
  • 56
    I believe the question is asking how to delete a specific named volume, not all of them. Is that possible? – mihow Jan 10 '19 at 00:23
68

There's no way to target the removal of a specific named volume with the docker-compose cli. Instead this can be achieved using the docker cli. See the docs.

Use docker volume ls to find the name of specific volume.

Remove the volume using docker volume rm VOLUME_NAME. You will need to have stopped and removed containers using the volume.

An example approach:

# Stop and remove container's using the target volume
docker-compose stop NAME_OF_CONTAINER

# We need the force flag, "-f", as the container is still bound to the volume
docker-compose rm -f NAME_OF_CONTAINER

# Next find your volume name in the following list
docker volume ls

# Finally remove the volume
docker volume rm VOLUME_NAME
jamsinclair
  • 1,257
  • 1
  • 12
  • 16
  • 3
    As far as I can tell, this is really the only way to remove a named volume and should be the accepted answer. Why they made it so damned hard, I don't fully understand. – CompEng88 Sep 28 '22 at 21:37
  • The command `docker-compose rm -fv pgsql` seems to delete both the container and the attached anonymous volumes – Hubro Jan 04 '23 at 11:44
  • It should read `docker compose stop ` not , right? – Splines Aug 23 '23 at 10:49
  • Note that you can combine stopping and removing the container (see the [docs](https://docs.docker.com/engine/reference/commandline/compose_rm/)): `docker compose rm -s `, where `-s` is short for `--stop`: (Stop the containers, if required, before removing) – Splines Aug 23 '23 at 10:56
34

Jan, 2022 Update:

This removes all the containers, networks, volumes and images defined in the docker-compose.

docker-compose down -v --rmi all

"-v" is for all the volumes

"--rmi all" is for all the images

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
8

I had the same issue as you, excepting I wanted to discard the working state of my grafana container while leaving the other containers running, which are running detached (ie. sudo docker-compose up -d). Here's the procedure I've come up with:

sudo docker-compose ps
sudo docker-compose stop grafana
sudo docker-compose rm --force grafana
sudo docker volume rm metricsmonitoring_grafana_data
sudo docker-compose up --force-recreate -d grafana

I don't know (without playing further) how best to determine the name of the docker volume to remove.

This is on docker-compose version 1.18.0

Cameron Kerr
  • 1,725
  • 16
  • 23