# Free disk space with docker system prune (safely)

> Docker prune commands remove unused objects such as stopped containers, dangling images and build cache to free disk space.

URL: https://levelrail.com/learn/docker/free-disk-space-with-docker-prune-safely/
Updated: 2026-11-27

To free disk space safely, run `docker system df` to see what uses it, then prune one kind at a time: `docker container prune`, `docker image prune` and `docker builder prune`. Leave volumes alone unless you have a backup, because a volume is where your data lives.

## How it works

Docker stores everything under `/var/lib/docker` on a Linux host. Four kinds of object take space:

| Object | What it is | Removed by |
| --- | --- | --- |
| Images | Layers pulled or built | `docker image prune` |
| Containers | Writable layers and logs of stopped containers | `docker container prune` |
| Build cache | Layers saved to speed up builds | `docker builder prune` |
| Volumes | Your data | `docker volume prune` (careful) |

Prune commands remove only **unused** objects. A **dangling** image is an untagged leftover (it shows as `<none>`), usually from an old build whose tag moved. An image is **in use** if any container, running or stopped, was created from it. That is why removing stopped containers first frees more images.

[Volumes](https://levelrail.com/learn/docker/docker-volumes-and-persistent-data/)) are the dangerous category. A volume counts as unused when no container references it, which includes a volume whose container you removed yesterday. That volume may hold your database. Container logs are a fifth source of growth: by default they are kept in a file per container with no size limit, and they are not removed by prune while the container exists.

### A worked example, start to finish

Picture a server that deploys by building an image under the same tag each time.

1. Deploy 1 builds `app:latest`. One image, one container.
2. Deploy 2 builds `app:latest` again. The tag moves to the new image, and the old one becomes a dangling image shown as `<none>`. The old container is removed, or stopped if you left it.
3. After many deploys, `docker image ls` shows a list of `<none>` images, and `docker builder prune` has many old cache entries.
4. `docker system df` shows large RECLAIMABLE numbers for images and build cache. `docker image prune` removes the dangling images, and `docker builder prune` clears the cache.

Nothing here touches a volume, which is why this sequence is safe on a server with a database.

## Step 1: See what is using space

```bash
docker system df
```

What you should see: a table with rows for Images, Containers, Local Volumes and Build Cache, each with a size and a RECLAIMABLE column. Add `-v` for a per-object breakdown.

Also check the disk itself with `df -h /`.

## Step 2: Remove stopped containers

```bash
docker container prune
```

It warns, asks for confirmation, and then removes every stopped container. Check `docker ps -a` first if a stopped container matters to you.

What you should see: the deleted container IDs and a line starting "Total reclaimed space".

## Step 3: Remove dangling images

```bash
docker image prune
```

By default this removes only dangling images. Adding `-a` removes every image not used by at least one container, including ones you pulled on purpose, which you will have to download again. To keep recent ones, add a filter such as `--filter "until=168h"`.

## Step 4: Remove build cache

```bash
docker builder prune
```

The next build is slower because it starts from scratch, but nothing breaks. Add `-a` to remove all unused cache instead of dangling cache alone.

## Step 5: Everything at once (optional)

```bash
docker system prune
```

This combines stopped containers, unused networks, dangling images and build cache. It asks for confirmation, and it does not remove volumes.

## Warning: volumes hold your data

```bash
docker volume ls
docker volume prune
docker volume prune --all
docker system prune --volumes
```

These delete volumes that no container references. On current Docker versions, `docker volume prune` removes only unused anonymous volumes, while `--all` also removes named ones. Older versions removed named volumes too, so do not rely on the lesser behaviour. A volume whose container was removed (for example by `docker compose down`) counts as unused, so its data is deleted for good. Take a backup first (see [Backups and the 3-2-1 rule](https://levelrail.com/learn/deploy-operate/backups-and-the-3-2-1-rule/))), and read the list before you confirm. Also be careful with `docker system prune -a`, which removes all unused images.

## Check it worked

Run `docker system df` again. The numbers for the cleaned categories should be smaller, and each prune command prints "Total reclaimed space". Then run `df -h /` to see the real disk. Finally `docker ps` and `docker volume ls`: your running containers and your volumes should be exactly as before.

## Edge cases and gotchas

- **A stopped container keeps its image alive.** `docker image prune -a` skips an image that a stopped container was created from. Run `docker container prune` first.
- **Prune does not shrink logs.** Container logs live with the container and can grow without limit. See [Limit Docker log size](https://levelrail.com/learn/deploy-operate/limit-docker-log-size/)).
- **A stopped stack looks unused.** If you ran [`docker compose down`](https://levelrail.com/learn/docker/docker-compose-basics/)) on an app you will bring back, its images and networks look unused. A later prune removes images the stack needs, and the next `up` pulls them again, which costs time and bandwidth.
- **`-f` skips the question.** `docker system prune -f` deletes without asking. Fine in a scheduled job once you trust it, risky when typed by hand.
- **Rebuild after cache prune is slow.** On a small server, a build from empty cache can take minutes and use memory.
- **Do not delete under `/var/lib/docker` by hand.** Docker tracks those files, and removing them directly can corrupt images.
- **Older versions differ.** The volume prune behaviour changed in recent releases. Check `docker volume prune --help` on your machine.

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `no space left on device` during pull or build | The disk is full | Run `docker system df`, then prune what is safe |
| Image prune frees almost nothing | Images are used by stopped containers | `docker container prune`, then prune images again |
| Disk still full after prune | Logs, volumes or non-Docker files | `sudo du -xh /var/lib/docker --max-depth=1`, and limit log size |
| `Error response from daemon: conflict: unable to remove repository reference` | A container still uses that image | `docker rm` the container first |
| Database gone after a prune | A volume was removed as unused | Restore from backup; in future back up before pruning volumes |
| Prune prompt shows more than you expected | You included `-a` or `--volumes` | Answer `n` and re-read the command |
| `docker system df` is small but the disk is full | Something else is large: logs, other applications, or files outside Docker | Use `df -h` and `sudo du -xh / --max-depth=1` to find it |
| A prune in a script waits forever | It is waiting for the y/n question | Add `-f` only after you have tested the command by hand |
| First build after pruning is slow | The build cache is gone | Expected, and it fills again |

## Real-world scenario

A small server with a 40 GB disk runs a few Compose apps. A deploy fails with `no space left on device`. You run `docker system df` and see that images and build cache account for most of the space, while the volume row is small and in use.

You run `docker container prune`, `docker image prune` and `docker builder prune` in that order, check `df -h /` and the apps still respond. You decide against `--volumes`, because one stopped app has a volume you might need. Then you add a weekly job that runs `docker image prune -f` and `docker builder prune -f`, and you set up [log limits](https://levelrail.com/learn/deploy-operate/limit-docker-log-size/)) so the logs stop growing.

## Quick reference

| Task | Command |
| --- | --- |
| Show usage | `docker system df`, `docker system df -v` |
| Stopped containers | `docker container prune` |
| Dangling images | `docker image prune` |
| Unused images older than a week | `docker image prune -a --filter "until=168h"` |
| Build cache | `docker builder prune` |
| Containers, networks, dangling images, cache | `docker system prune` |
| Volumes (data loss risk) | `docker volume prune`, `docker system prune --volumes` |

## Security and safety notes

- Volume prune and `docker system prune --volumes` delete data permanently. Back up first and read the list.
- Never schedule a prune with `-a` or `--volumes` on a server whose stacks are sometimes stopped.

## Related lessons

- [Image, container and registry explained](https://levelrail.com/learn/docker/image-container-registry-explained/)): what dangling and unused images are
- [Docker volumes and persistent data](https://levelrail.com/learn/docker/docker-volumes-and-persistent-data/)): why volumes need care
- [Limit Docker log size](https://levelrail.com/learn/deploy-operate/limit-docker-log-size/)): stop logs from filling the disk
- [Check disk, memory and CPU usage](https://levelrail.com/learn/server-basics/check-disk-memory-and-cpu-usage/)): find what else uses space

## Next

Make Docker notice a broken app with [Docker healthchecks](https://levelrail.com/learn/docker/docker-healthchecks/)).

## Frequently asked questions

### Is docker system prune safe?

Without extra flags it removes stopped containers, unused networks, dangling images and build cache. It does not remove volumes. Read the confirmation prompt before you answer y.

### Why did my database data disappear?

Most likely a volume prune removed a volume after its container was deleted, for example after docker compose down. Volumes hold data, and that deletion cannot be undone.

### Docker said it reclaimed space but df still shows the disk full. Why?

A large share may be container logs or volumes, which system prune does not touch, or space held by files that a running process still has open. Check docker system df -v and the size of /var/lib/docker.

### Can I delete files under /var/lib/docker by hand?

No. Docker tracks them internally, and removing files directly can corrupt images and containers. Use the docker prune and rm commands.


## Sources

- [Docker docs, prune unused Docker objects](https://docs.docker.com/engine/manage-resources/pruning/)
