Sub topic 10
One command for everything: docker system prune
This chapter assumes you can run Docker commands in a terminal. It also assumes you know the difference between an image and a container, and that you have used docker system df and the individual prune commands from earlier chapters. Here you will learn what docker system prune removes, what it skips, and how to run it without losing data.
What the command does
docker system prune runs several single-purpose prune operations in one go. By default it removes:
- all stopped containers
- all networks not used by at least one container
- all dangling images (untagged images that no tagged image depends on)
- build cache that is not in use
It does not remove volumes, and it does not remove tagged images that are merely unused. Both exclusions are deliberate. Volumes hold data, and an unused tagged image is something you may want to run again without a pull.
docker system pruneDocker prints a warning that lists what will be removed, then waits for you to type y. When it finishes, it prints Total reclaimed space, which you can compare against the RECLAIMABLE column of docker system df.
The order of operations
The command works through the object types in a fixed order: containers first, then networks, then volumes (only if requested), then images, then build cache. The order has two consequences.
- Images freed in the same run. An image used by a stopped container counts as in use. Because stopped containers are removed first,
docker system prune -acan also remove the images those containers were using, in one pass. - Volumes freed in the same run. A volume attached only to a stopped container counts as in use until that container is removed. With
--volumes, the container is removed first, so the volume then looks unused and is deleted too.
The second consequence is the main way people lose data with this command.
Flags that change the scope
| Flag | Effect | Risk |
|---|---|---|
| (none) | Stopped containers, unused networks, dangling images, unused build cache | Low. Containers lose their writable layer. |
| `-a`, `--all` | Also removes every image not used by a container | Medium. The next `run` or `build` must pull or rebuild. |
| `--volumes` | Also removes volumes not used by a container | High. Can delete data permanently. |
| `-f`, `--force` | Skips the confirmation prompt | Removes the last safety check. |
| `--filter` | Limits removal by age or label | Reduces scope. See below. |
Flags combine. docker system prune -a --volumes is the most aggressive form. It leaves the daemon with only the objects that running containers use.
Volumes and Docker versions
Behavior for volumes changed in Docker Engine 23.0. Since that release, docker volume prune and docker system prune --volumes remove only anonymous volumes (those with no name you chose). Named volumes are removed only if you also pass -a. Older engines remove all unused volumes with --volumes.
Check your version with docker version before relying on either behavior. On an older engine, assume --volumes can delete your named database volumes.
docker version --format '{{.Server.Version}}'Filtering by age
The until filter restricts removal to objects created before a given time. It accepts a duration such as 168h or a timestamp.
docker system prune -f --filter "until=168h"This removes stopped containers, unused networks, dangling images, and unused build cache older than 168 hours (seven days). It suits scripts because -f suppresses the prompt. Fresh objects survive, so the image you built an hour ago is not deleted.
To remove all unused images (not only dangling ones) older than seven days, add -a:
docker system prune -a -f --filter "until=168h"The label filter selects objects by label. label=keep limits pruning to objects that have the label keep. label!=keep prunes everything except objects that carry it, which gives you a way to protect specific objects:
docker system prune -f --filter "label!=keep"Protection only works for objects that were created with the label. For example, you can label an image at build time with docker build --label keep=true . or use LABEL keep=true in the Dockerfile.
docker run -d --label keep=true --name important nginxTwo limits apply to filters.
- The
untilfilter is not available for volumes. Do not expect--volumescombined withuntil=168hto protect recent volumes. Check your engine's behavior or prune volumes separately. - The
untilfilter compares creation time, not last use. An image you pulled eight days ago and use every day is still "older than seven days" if no container is running it right now.
Previewing, because there is no dry run
docker system prune has no --dry-run option. The confirmation prompt gives only a summary of categories, and it does not list which volumes will go. List candidates yourself before running the command:
docker ps -a --filter status=exited
docker ps -a --filter status=created
docker image ls --filter dangling=true
docker volume ls --filter dangling=true
docker network ls
docker system dfPay particular attention to docker volume ls --filter dangling=true. A volume is dangling when no container, running or stopped, references it. This is the list docker volume prune will act on. Remember the ordering rule from earlier: volumes belonging only to stopped containers are not in this list now, but they become eligible once the prune removes those containers. Review docker ps -a as well, and check which volumes the stopped containers mount.
docker ps -a --format '{{.Names}}\t{{.Status}}\t{{.Mounts}}'Other things that surprise people
- Containers in the
createdstate. A container that was created but never started is stopped, so prune removes it. - Compose projects. After
docker compose stop, the containers still exist, so networks and volumes are in use. Prune removes the stopped containers, and the nextdocker compose upcreates new ones. Anything stored in the container's writable layer is lost. Data in named volumes survives only if you did not pass--volumes. - Build cache after a prune. The next build runs slower because layers are rebuilt. This is the usual cost of reclaiming space, not a sign that something broke.
- Shared daemons. Prune acts on the whole daemon, not on your project. On a shared CI runner or a shared development host, you can delete another person's stopped containers or cached images. Use labels or run prune only on hosts you own.
- Scripts and exit status. With
-f, the command exits 0 even when nothing was removed. Check theTotal reclaimed spaceline if you need to log the result.
Choosing the right form
| Situation | Command |
|---|---|
| Routine cleanup on a development machine | `docker system prune` |
| Disk is nearly full, images can be pulled again | `docker system prune -a` |
| Scheduled cleanup in a script | `docker system prune -f --filter "until=168h"` |
| You know a volume is abandoned | Remove it by name with `docker volume rm`, or run `docker volume prune` after previewing |
| Host runs databases in stopped containers | Prune each object type separately and skip volumes |
A scheduled run keeps a host tidy without touching data. A crontab entry that runs every Sunday at 03:00 looks like this:
0 3 * * 0 docker system prune -f --filter "until=168h"It omits --volumes, so volumes are never touched by the schedule.
Summary of safe habits
- Run
docker system dffirst, so you know what is worth reclaiming. - List candidates with the
--filterlisting commands from the preview section. - Start with plain
docker system prune, and add-aonly when you need more space. - Treat
--volumesas a separate, deliberate decision, and take a backup of anything you cannot recreate. - Use
untilin unattended runs so that recent work is kept.
Exercise
A stopped container named pg is the only container that mounts the named volume pgdata. You run docker system prune -a --volumes on Docker Engine 25. What happens to pgdata, and what would you run instead to reclaim space safely?
Show answer
Docker removes the stopped container pg first. The volume pgdata then has no references, and because -a is present, Docker Engine 25 removes named volumes as well, so pgdata and its data are deleted. To reclaim space safely, run docker system prune -a without --volumes, or docker system prune -f --filter "until=168h" for a lighter cleanup. Volumes are untouched in both cases. Preview candidates with docker volume ls --filter dangling=true and docker ps -a, and back up pgdata before any volume prune.
Exercise
You want a weekly cron job that removes stopped containers, unused networks, dangling images, and unused build cache older than seven days, without prompting and without deleting volumes. Write the crontab line.
Show answer
0 3 * * 0 docker system prune -f --filter "until=168h". It runs Sundays at 03:00. The -f flag skips the prompt, the until=168h filter keeps anything newer than seven days, and leaving out --volumes keeps all volumes.