KhueApps
Home/DevOps/Fix Docker error: volume is in use and cannot be removed

Fix Docker error: volume is in use and cannot be removed

Last updated: October 06, 2025

Overview

You’ll see this when trying to delete a named volume that one or more containers or services still reference:

Error response from daemon: volume is in use and cannot be removed

The fix is to find what is using the volume, stop/remove it, then remove the volume. This guide covers Docker CLI, Compose, and Swarm.

Quickstart

  • Identify which containers reference the volume
  • Stop and remove those containers (or bring down the Compose project/service)
  • Remove the volume

Minimal commands (replace V with your volume):

# 1) Find referencing containers
docker ps -a --filter volume=V

# 2) Stop and remove them
docker stop $(docker ps -q --filter volume=V) 2>/dev/null || true
docker rm   $(docker ps -aq --filter volume=V) 2>/dev/null || true

# 3) Remove the volume
docker volume rm V

For Compose projects, run from the project directory:

docker compose down -v

Minimal working example

This example reproduces the error and shows the fix.

# compose.yaml
services:
  app:
    image: busybox:1.36
    command: sh -c "echo hello >> /data/log.txt && sleep 300"
    volumes:
      - mydata:/data
volumes:
  mydata:
# Start the service (creates and uses the named volume "mydata")
docker compose up -d

# Attempt to remove the volume while it’s in use
docker volume rm mydata
# => Error response from daemon: volume is in use and cannot be removed

# Correct fix: bring the project down and remove volumes
docker compose down -v

# Now the volume is gone
docker volume ls | grep mydata || echo "mydata removed"

Step-by-step diagnosis and fix

  1. Confirm the volume exists and inspect it
  • List volumes:
docker volume ls
  • Inspect for details (driver, mountpoint, refcount):
docker volume inspect V
  1. Find containers referencing the volume
  • Via ps filter:
docker ps -a --filter volume=V
  • If you know a container, verify its mounts:
docker inspect C | grep -A3 Mounts
  1. Stop and remove referencing containers
  • Stop and remove by volume filter:
docker stop $(docker ps -q --filter volume=V) 2>/dev/null || true
docker rm   $(docker ps -aq --filter volume=V) 2>/dev/null || true
  • As a last resort, force-remove containers:
docker rm -f $(docker ps -aq --filter volume=V) 2>/dev/null || true
  1. Remove the volume
docker volume rm V

If your Docker version supports it, a force flag may exist. Use with caution; ensure no needed data remains and understand that containers referencing the removed volume may fail later.

Docker Compose specifics

  • Compose often creates project-scoped volume names like projectname_mydata. Confirm the actual name:
docker volume ls | grep mydata
  • From the Compose project directory:
docker compose down -v  # removes containers, networks, and named volumes created by this project
  • If you created external volumes (external: true), compose down -v will not remove them. Remove them explicitly with docker volume rm.

  • Removing only the service without -v leaves volumes intact; they will still be “in use” if containers remain.

Swarm services specifics

Swarm services can hold a volume reference even if no traditional container is visible.

  • Identify services that may mount the volume:
docker service ls
# Inspect a service for mounts
docker service inspect S --pretty | sed -n '/Mounts/,$p'
  • Temporarily scale down or remove the service:
docker service scale S=0
# or
docker service rm S
  • Then remove the volume:
docker volume rm V

Cleaning up anonymous/unused volumes safely

Anonymous volumes accumulate when containers are recreated. To remove volumes not referenced by any container:

docker volume prune
# add -f to skip prompt; consider --filter until=<timestamp> or label filters

Compose can remove project volumes created by it:

docker compose down -v

Common pitfalls

  • Wrong volume name: Compose renames volumes with a project prefix. Confirm actual names via docker volume ls.
  • Stopped containers still count: A stopped container referencing V still blocks removal. Remove it, don’t just stop it.
  • Data loss: Removing a volume deletes its data. Back up first if needed.
  • Confusing bind mounts vs named volumes: docker volume rm only applies to named volumes. Bind mounts are host paths; delete the host directory instead (once no container uses it).
  • Multiple projects/use-cases: The same named volume can be shared. Ensure all referencing containers/services are removed.

Performance notes

  • Use filters to avoid scanning the entire fleet: docker ps -a --filter volume=V is faster and safer than grepping huge lists.
  • Prefer targeted cleanup over global prune in busy environments. Volume prune can be slow and disruptive on hosts with many volumes.
  • Large volumes delete faster with the local driver (a directory removal), but drivers backed by network storage may take longer. Avoid running mass deletions during peak I/O.

Tiny FAQ

  • Why do I still get the error after stopping a container? Because the stopped container still references the volume. Remove the container, then remove the volume.

  • How do I see which containers use a volume? docker ps -a --filter volume=V lists them. You can also inspect specific containers for Mounts.

  • How do I remove all volumes not in use? docker volume prune removes volumes with no container references.

  • Does force-removing a volume affect containers? It can. Containers that referenced the deleted volume may fail to start or behave unexpectedly later. Use it only when you’re certain the data is expendable.

  • Where is a local volume’s data on Linux? Usually under /var/lib/docker/volumes/V/_data (implementation-specific). Access only when you know the implications.

Series: Docker

DevOps