Sub topic 9
Filtering Prunes by Age and Label
This chapter assumes you can run docker ps, docker images, and docker build, and that you know the difference between an image and a container. It also assumes you have used a plain prune command such as docker container prune or docker image prune. Here you will restrict those commands so they delete only old objects, or only objects with a chosen label.
Why filter a prune
A plain prune removes every object of its kind that is unused. That is fine on a personal laptop. It is risky on a shared build host, where the "unused" image you just pulled for tomorrow's deploy or the stopped container someone is about to inspect is also a candidate.
The --filter option narrows the candidates. Two filters cover most needs:
untilselects objects older than a cutoff.labelselects, or protects, objects by a label you attached.
The --filter syntax
Every prune command accepts --filter (short form -f is taken by --force, so always write --filter). The value is a key=value pair, and you can repeat the option.
docker container prune --filter "until=24h"
docker image prune --filter "label=stage=test"Docker checks filter names. If a command does not support the filter you pass, it normally stops with an "Invalid filter" error instead of ignoring it. Read that error as a warning that your cleanup would not have been limited the way you intended.
Filtering by age with until
Accepted formats
The until filter takes one of three formats:
| Format | Example | Meaning |
|---|---|---|
| Go duration | `24h`, `90m`, `1h30m` | Older than this long before now |
| Date-formatted timestamp | `2026-11-01T00:00:00` | Older than this moment |
| Unix timestamp | `1793491200` | Older than this many seconds since 1970 |
Go durations have no day or week unit. until=7d fails. Write seven days as 168h and thirty days as 720h. Durations are measured against the clock of the machine running the Docker daemon, which matters when you use a remote daemon with a skewed clock. A timestamp without a zone is interpreted in local time, so a Unix timestamp is the least ambiguous choice in scripts.
# GNU date (Linux): everything older than 30 days
docker image prune -a --filter "until=$(date -d '30 days ago' +%s)"
# macOS (BSD date)
docker image prune -a --filter "until=$(date -v-30d +%s)"What "age" means
Age is the object's creation time, not the last time it was used or pulled.
- For a container, it is when the container was created, not when it stopped.
- For an image, it is the build time recorded inside the image.
The image rule surprises people. Suppose you pull postgres:16 today, but its maintainers built it ten days ago, and no container uses it. The command below removes it, even though it landed on your disk minutes earlier.
docker image prune -a --filter "until=168h"The -a flag matters here. Without it, image prune touches only dangling images, so an unused but tagged image survives whatever until value you give. With -a, any image not used by a container is a candidate, and until then limits it to those created more than 168 hours ago.
The same logic applies to stopped containers. A container created three weeks ago that exited five minutes ago is still removed by until=24h. If you need to protect recently stopped containers, add a label (next section) or review them first.
Filtering by label
A label is a key, with an optional value, that you attach to an object. It lets you mark objects as disposable or as protected.
Setting labels
Labels are fixed when the object is created. You cannot add one to an existing container or image, so plan them into the commands you already use.
# A container
docker run --name scratch1 --label env=scratch alpine true
docker run --name keeper --label retain=true alpine true
# An image, from the command line
docker build --label stage=test -t demo:test .In a Dockerfile, use LABEL:
FROM alpine:3.20
LABEL stage="test"Images built from that Dockerfile carry stage=test. Compose files have a labels: key for services, networks, and volumes.
Selecting and protecting
The label filter has four forms:
| Filter | Matches objects that |
|---|---|
| `label=env` | have the key `env`, with any value |
| `label=env=scratch` | have `env` set to `scratch` |
| `label!=retain` | do not have the key `retain` |
| `label!=retain=true` | do not have `retain` set to `true` |
Use single quotes around the != forms so your shell never tries history expansion on !.
# Remove only stopped containers marked as scratch
docker container prune --filter 'label=env=scratch'
# Remove all stopped containers except those marked retain=true
docker container prune --filter 'label!=retain=true'The second form is a protection list. Anything you label retain=true survives routine cleanup, and everything else is removed. On a shared host, agree on one protection label and add it to anything that must outlive a cleanup.
Which prunes accept which filters
Support differs by command and by Docker version, so confirm with --help on your system. The usual picture:
| Command | `until` | `label` |
|---|---|---|
| `docker container prune` | yes | yes |
| `docker image prune` | yes | yes |
| `docker network prune` | yes | yes |
| `docker builder prune` | yes | no (it has its own filters, such as `type`) |
| `docker volume prune` | no | yes |
| `docker system prune` | yes | yes |
Volumes deserve extra care. Volume creation time says nothing about whether the data inside is still needed, which is why volume prune takes no until. On Docker Engine 23.0 and later, volume prune removes only anonymous volumes unless you pass --filter all=true, which also includes unused named volumes. Check docker volume prune --help to see which behavior your engine has before you rely on it.
Combining filters and using them with system prune
You can pass --filter more than once. Filters of different kinds narrow the result together, so an object must satisfy all of them to be removed.
# Unused images older than 14 days that are labeled stage=test
docker image prune -a \
--filter "until=336h" \
--filter "label=stage=test"docker system prune forwards its filters to each kind of object it cleans: containers, networks, images, and build cache.
docker system prune -f --filter "until=168h" --filter 'label!=retain=true'Two cautions apply to system prune:
- One filter applies to several object kinds at once. A label that exists on your images may not exist on your networks, so a
label=selector can make the command skip some kinds entirely, while alabel!=protector leaves unlabeled objects as candidates. - If you add
--volumes, do not count onuntilto protect volumes, because of the reasoning above. Prune volumes in a separate, deliberatedocker volume prunecommand with its ownlabelfilter.
Previewing a filtered prune
Prune commands have no dry-run flag, and ls commands do not accept until. You can still reproduce most selections by listing with a format that shows creation time.
# Stopped containers with their creation time and labels
docker ps -a --filter status=exited \
--format '{{.ID}} {{.CreatedAt}} {{.Names}} {{.Labels}}'
# Images with age, so you can eyeball the cutoff
docker image ls --format '{{.ID}} {{.CreatedSince}} {{.Repository}}:{{.Tag}}'
# Label filters do work with ls
docker ps -a --filter 'label=env=scratch'
docker image ls --filter 'label=stage=test'Without -f, each prune also prints a confirmation prompt that names the kind of object, which is your last chance to cancel. In scripts, run the listing first and log its output, so you can later see what a scheduled run removed.
Common errors
until=7dis rejected. Go durations stop at hours. Use168h.- Nothing is removed by
image prune --filter until=.... You omitted-a, so only dangling images were considered. Add-aif you mean all unused images. - A fresh pull disappears. Image age is build time, not pull time. Label images you want to keep, or skip
untiland review withls. - "Invalid filter" on
volume prune. You useduntil. Uselabelinstead. - A protected object was removed. Labels are set at creation. Check with
docker inspect --format '{{json .Config.Labels}}' <container>that the label was really applied, and note that an image's labels are separate from those of containers started from it. - A filter that matches nothing "succeeds". A prune with no matches finishes with zero reclaimed space and no error. Check that the filter value is spelled right by running the matching
lscommand first.
Exercise
A build host has two stopped containers. ci-old was created 10 days ago and carries the label retain=true. ci-new was created 2 hours ago with no labels. You run docker container prune -f --filter "until=168h" --filter 'label!=retain=true'. Which containers are removed, and why?
Show answer
Neither is removed. ci-old is older than 168 hours but is protected by retain=true, so it fails the label condition. ci-new has no retain label, but it was created only 2 hours ago, so it fails the until condition. A container is removed only if it passes both filters.
Exercise
You want to remove every unused image created more than 30 days ago, including tagged ones, and you want to keep any image that has the label retain=true. Write the command.
Show answer
docker image prune -a --filter "until=720h" --filter 'label!=retain=true'. The -a flag includes tagged but unused images, 720h is 30 days expressed as a Go duration, and the label!= filter protects labeled images.