> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Why docker rm Doesnt Free All Disk Space

> Explains why docker rm doesn't free disk space due to leftover volumes, how anonymous volumes accumulate in CI, and shows commands and best practices to find and remove them.

Let's walk through a common CI/build-server problem and why deleting containers with `docker rm` does not always release all disk space.

Scenario

* Your application stores data (for example, customer orders) in a PostgreSQL database.
* You run Postgres in one container and the application in another.
* Every time code changes, your CI pipeline:
  * starts a fresh Postgres container and an application container on the build server,
  * runs tests that insert data,
  * stops both containers and deletes them with `docker rm`.

After a couple of weeks you discover \~100 GB used on the build server, but `docker ps -a` shows no containers. Where did the disk space go? The answer is volumes. Here's the step-by-step explanation.

What happens when Docker creates a container

* Docker pulls the Postgres image; image data is stored as immutable layers on disk and shared by all containers made from that image.
* When a container is created, Docker adds a thin writable layer on top of the image. Temporary files and non-declared-volume data end up in this writable layer.

![A dark-themed diagram titled "Step by Step" showing a build server disk usage bar (with a marker for 100 GB used) and a container graphic containing a purple "Writable Layer" above a Postgres image. Labels like "Build Server Disk," "Test Pipeline," "Creates," and "Changes" are also visible.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Docker/Why-docker-rm-Doesnt-Free-All-Disk-Space/build-server-disk-writable-layer-postgres.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=ca950dca8ad10df5b7c890664276a608)

Why database files are stored in volumes

* Databases like Postgres keep data in files under their data directory (for example `/var/lib/postgresql/data`).
* The official Postgres image's Dockerfile declares a `VOLUME` for the data directory (see Dockerfile `VOLUME` documentation). That tells Docker to mount that path as a volume so data persists outside the container's writable layer.
* If you run the image without specifying a host bind or named volume, Docker creates an anonymous volume (a volume with a generated name) for that declared mount point.

Each CI run that inserts test rows writes into an anonymous volume instead of the container's writable layer. Over many CI runs, each run can create a new anonymous volume that accumulates hundreds of megabytes, eventually adding up to tens of gigabytes on the build server.

![A stylized diagram of container storage on a build server, with a capacity bar at the top showing 100 GB used. It shows a Postgres image with a writable layer and an anonymous volume mounted at /var/lib/postgresql containing an "orders" data folder.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Docker/Why-docker-rm-Doesnt-Free-All-Disk-Space/container-storage-postgres-orders-writable-100gb.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=25c3a5ac09c9b5ab22277302e93ec29d)

What `docker rm` removes (and what it doesn't)

* `docker rm` deletes the container and its writable layer.
* Docker intentionally leaves volumes on disk because volumes are designed to persist data beyond a container's lifetime.
* If your CI created a new anonymous volume each run, `docker rm` will not delete those volumes — you will accumulate many unused anonymous volumes that consume disk space.

A simplified view of the state:

```text theme={null}
100 GB used   <-- build server before cleanup
docker rm     <-- removes containers + writable layers only
/var/lib/postgresql
500 MB        <-- per-run database data persisted in anonymous volumes
```

Inspecting the system: `docker ps -a` may show no containers, and `docker system prune` will not remove volumes unless explicitly told to.

![A dark-themed dashboard showing a "Build Server Disk" usage bar (highlighting 100 GB used) alongside panels for a Postgres image and an "Anonymous Volume" data folder labeled 500 MB.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Docker/Why-docker-rm-Doesnt-Free-All-Disk-Space/build-server-disk-100gb-postgres-500mb.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=f901ae2659ca315b56d55816dbf1e1fd)

How to find and free the space

1. See where space is used

```bash theme={null}
docker system df
```

Check the VOLUMES row to see how much disk space volumes occupy.

2. List volumes (optional)

```bash theme={null}
docker volume ls
docker volume ls -f "dangling=true"   # show anonymous/dangling volumes
```

3. Remove unused (dangling) volumes

```bash theme={null}
docker volume prune
```

Example output:

```bash theme={null}
$ 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:
cf1962cdaa5767d8e63f010e6778ee0ef4f02744f525054a19923ee86e61afe3
...
Total reclaimed space: 98.5GB
```

4. Make `system prune` also remove unused volumes

```bash theme={null}
docker system prune --volumes
```

Note: `docker system prune` without `--volumes` will skip volumes by default.

Quick reference table: common cleanup commands

| Command | Purpose |
| -: | - |
| `docker system df` | Show disk usage including images, containers, and volumes |
| `docker volume ls` | List all volumes |
| `docker volume ls -f "dangling=true"` | Show anonymous/dangling volumes |
| `docker volume prune` | Remove all unused volumes (prompts) |
| `docker system prune --volumes` | Remove unused containers, images, networks, and volumes |

Best practices to avoid disk bloat in CI

* Remove container-associated anonymous volumes when deleting containers:

```bash theme={null}
docker rm -v <container-id-or-name>
```

* Run an explicit cleanup step at the end of CI jobs (safe for ephemeral agents):

```bash theme={null}
docker volume prune -f
# or, for aggressive cleanup on ephemeral agents:
docker system prune -a --volumes -f
```

* Avoid using images that declare `VOLUME` for ephemeral CI databases, or explicitly mount a known named volume or host directory that your CI can remove.
* Prefer targeted cleanup (specific volumes or named resources) in shared/production environments to avoid accidental data loss.

Be careful when pruning volumes or using `--volumes` — you can permanently delete production data. Always ensure you prune only in the intended environment (for example, ephemeral CI agents) or back up important volumes first.

`docker run --rm` removes the container automatically when it exits, but it does not remove named or anonymous volumes created for that container. To ensure volumes are removed, either remove the container with `docker rm -v` or run dedicated volume cleanup steps after your CI job.

Further reading and references

* Dockerfile `VOLUME` documentation: [https://docs.docker.com/engine/reference/builder/#volume](https://docs.docker.com/engine/reference/builder/#volume)
* Postgres Docker image: [https://hub.docker.com/\_/postgres](https://hub.docker.com/_/postgres)
* Docker CLI cleanup docs: [https://docs.docker.com/engine/reference/commandline/system\_prune/](https://docs.docker.com/engine/reference/commandline/system_prune/)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/1d3d5877-dbf7-4105-8bc2-2c619ac62421/lesson/7dbcbb17-9ebb-4660-a20f-fc249424ad37)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.