Sysadmin

Docker Disk Space: Prune, Logs, and Unused Images

Docker Disk Space: Prune, Logs, and Unused Images

To free up Docker disk space, first measure with docker system df, then remove what is unused: docker system prune deletes stopped containers, unused networks, dangling images and dangling build cache; docker system prune -a also deletes every image no container uses. Container logs are not part of either command: they are cleaned by limiting them with max-size and max-file in /etc/docker/daemon.json, and on an existing container by truncating the log file, never by deleting it.

Every command and output below comes from a test run on 1 November 2026 with Docker Engine 29.9.2 (containerd 2.4.2, buildx 0.38.1, Compose 5.6.1), inside an isolated Docker-in-Docker container, plus a second daemon with the classic overlay2 storage to compare. Outputs are real, shortened where marked with “…”. Some behaviours turned out different from what most guides, including the earlier version of this one, say.

Docker cleanup in 60 seconds

docker system df                          # where the space goes (logs are NOT included)
docker system df -v                       # per image, container, volume, cache entry
docker container prune -f                 # stopped containers
docker image prune -a -f                  # images not used by any container
docker builder prune -a -f                # all build cache
docker volume prune -f                    # anonymous volumes not in use (named ones survive)
sudo du -sh /var/lib/docker/containers/*/*-json.log | sort -h | tail   # biggest logs

If you only need one line on a build server: docker system prune -a -f. On a host with databases, read the volume section before adding --volumes.

Measure first: docker system df and docker system df -v

On the test daemon, with a Redis container, a few stopped containers, three volumes and a locally built image:

$ docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          5         3         181.8MB   105MB (57%)
Containers      4         2         20.48kB   12.29kB (60%)
Local Volumes   3         2         52.43MB   31.46MB (60%)
Build Cache     7         0         117MB     37.07kB

RECLAIMABLE is what a prune could free. -v breaks it down item by item, which is how you find the one volume or image that matters:

$ docker system df -v
Images space usage:

REPOSITORY   TAG        IMAGE ID       CREATED          SIZE      SHARED SIZE   UNIQUE SIZE   CONTAINERS
demo         latest     e6c67d38fb51   About a minute   117MB     12.09MB       104.9MB       0
redis        7-alpine   8da19c7399dc   5 days ago       57.8MB    12.09MB       45.7MB        1
alpine       3.20       d9e853e87e55   6 months ago     12.2MB    0B            12.18MB       1
…
Local Volumes space usage:

VOLUME NAME                                                        LINKS     SIZE
redisdata                                                          1         0B
ea7bbeaaaca82d0b735f607fda3796d0ad2f4b71ca0c92e5f6b1199d709cb176   1         20.97MB
oldvol                                                             0         31.46MB

Build cache usage: 117MB
CACHE ID       CACHE TYPE     SIZE      CREATED          LAST USED        USAGE     SHARED
bubvhvi8ee54   regular        105MB     About a minute   About a minute   3         true
…

Three things to read in that output: UNIQUE SIZE is what you actually get back by deleting an image (shared layers stay if another image uses them); LINKS 0 marks a volume no container references; a volume with a 64-character hex name is anonymous.

The trap: logs are invisible to docker system df

I then started a container that writes three million lines to stdout. Twenty-five seconds later:

$ sudo du -sh /var/lib/docker/containers/*/*-json.log | sort -h | tail -2
4.0K    /var/lib/docker/containers/a7849edd…/a7849edd…-json.log
277.0M  /var/lib/docker/containers/98addd08…/98addd08…-json.log

$ docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          5         3         181.8MB   105MB (57%)
Containers      4         2         20.48kB   12.29kB (60%)
…

277 MB of log and the Containers row still says 20 kB. When df -h says the disk is full and docker system df shows little, the logs are the first suspect. To map each log to its container name:

for c in $(docker ps -aq); do
  printf '%s %s\n' "$(docker inspect -f '{{.Name}}' $c)" \
    "$(sudo du -h "$(docker inspect -f '{{.LogPath}}' $c)" | cut -f1)"
done
/chatty 1.7M
/batch-old 0
/oneshot 0
/cache 4.0K

What each prune removes: with and without -a and --volumes

The quickest way to know what a prune will do is to let it ask: without -f every prune prints its scope and waits. Answering n removes nothing. These are the warnings printed by Docker 29.9.2:

### docker system prune
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - unused build cache

### docker system prune -a
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache

### docker system prune -a --volumes
  - all stopped containers
  - all networks not used by at least one container
  - all anonymous volumes not used by at least one container
  - all images without at least one container associated to them
  - all build cache

### docker volume prune
WARNING! This will remove anonymous local volumes not used by at least one container.

### docker volume prune -a
WARNING! This will remove all local volumes not used by at least one container.
Command Containers Images Build cache Volumes
docker system prune stopped dangling only dangling none
docker system prune -a stopped all unused all none
docker system prune -a --volumes stopped all unused all anonymous, unused
docker image prune – dangling only – –
docker image prune -a – all unused – –
docker volume prune – – – anonymous, unused
docker volume prune -a – – – all unused, named too
docker builder prune – – dangling –
docker builder prune -a – – all –

Running containers, the images they use and the volumes they mount are never touched, with or without -a or -f. -f only skips the confirmation. A “docker full prune” is therefore docker system prune -a --volumes, and even that leaves your named volumes alone. The official pruning page describes the same scope.

docker image prune: dangling vs unused images

A dangling image is one with no tag, usually the previous version left behind when you rebuild app:latest. An unused image is any image no container (running or stopped) is created from, tagged or not. docker image prune removes only the first kind; docker image prune -a removes both.

On Docker 29 you may not see any dangling image

Fresh installs of Docker Engine 29 use the containerd image store (docker info shows driver-type: io.containerd.snapshotter.v1). In my test I rebuilt app:latest twice with different content, and no untagged image appeared:

$ docker images -f dangling=true
IMAGE   ID             DISK USAGE   CONTENT SIZE   EXTRA

$ docker image prune -f
Total reclaimed space: 0B

The old layers were not gone: they sat in the build cache (see below). On the second daemon, started with --feature containerd-snapshotter=false and the classic overlay2 driver, the same rebuilds produced the familiar untagged images:

$ docker images -f dangling=true
IMAGE        ID             DISK USAGE   CONTENT SIZE   EXTRA
<untagged>   3b0936ca1806       60.2MB             0B
<untagged>   9404080b8ced       60.2MB             0B

$ docker image prune -f
Deleted Images:
deleted: sha256:9404080b8cedbc7c31c84a964bfc5d12f1fa98ed4e6864e3854840d310df6b08
deleted: sha256:3b0936ca180657bc9b557704e91edfef4862d48451892b1ba0b9981c1d88192f

Total reclaimed space: 0B

Again 0 B: the dangling images shared their big layer with the build cache, so deleting the image record freed nothing. The space came back only with docker builder prune. Hosts upgraded from older versions keep overlay2 until you switch them, so check which store you have before comparing numbers.

Remove all unused images, or only some

$ docker image prune -a -f --filter label=project=demo
Deleted Images:
untagged: demo:latest
deleted: sha256:e6c67d38fb51e985ac2cc0d9d2781c8de9a03d7daf64621842d50029ff169a67
…
untagged: app:latest
…
Total reclaimed space: 94.41MB

Filters accepted by image prune: until and label (also negated: label!=keep). Labels are the safest way to scope a prune on a shared host: put LABEL project=… in your Dockerfiles and prune per project.

The until filter is not the image’s creation date

This one surprised me. alpine:3.20 shows “Created 6 months ago”, yet:

$ docker image inspect -f '{{.Created}}' alpine:3.20
2026-04-16T23:53:26.803599608Z
$ docker image inspect -f '{{json .Metadata}}' alpine:3.20
{"LastTagTime":"2026-11-01T08:39:58.634163814Z"}

$ docker image prune -a -f --filter until=720h
Total reclaimed space: 0B

$ docker image prune -a -f --filter until=5m
Deleted Images:
untagged: alpine:3.20
…
untagged: alpine:3.21
Total reclaimed space: 12.19MB

With until=720h (30 days) nothing went, with until=5m both Alpine images went: the filter compared against the moment the image arrived on this host (pulled about 15 minutes earlier), not the build date in the image. A nightly --filter until=168h therefore keeps everything pulled in the last week, whatever its age. That is usually what you want, but it is not what the column CREATED suggests.

Stopped containers: docker container prune

$ docker container prune -f --filter label=env=test
Deleted Containers:
02ce3769baa914b497dc587fb0af0414379517bc289c5ad97c0f33c7542bf5f1
02a23c272891f9bb7a976ea430da6e9749e3d658b3f3564873af617f86e65404

Total reclaimed space: 8.192kB

The space a stopped container holds is its writable layer plus its log. In docker system df you see only the first one, but removing the container deletes its log too: in the final full prune of my test, the 313 MB log of a removed container disappeared from disk while the “Total reclaimed space” line did not count it.

Removing a container does not remove its volumes. The anonymous volume of the oneshot container stayed on disk after container prune and was only deleted by the volume prune that followed. Use docker rm -v when you want both in one step.

Volumes: what docker volume prune deletes since Docker 23

Since Docker Engine 23.0 (API 1.42, when --all was added), docker volume prune deletes only anonymous volumes. Many guides and scripts still assume it removes every unused volume. Test with one anonymous volume, a named volume oldvol no longer used, a named volume pgbackup labelled keep=true, and redisdata mounted by a running container:

$ docker volume ls -f dangling=true
DRIVER    VOLUME NAME
local     ea7bbeaaaca82d0b735f607fda3796d0ad2f4b71ca0c92e5f6b1199d709cb176
local     oldvol
local     pgbackup

$ docker volume prune -f
Deleted Volumes:
ea7bbeaaaca82d0b735f607fda3796d0ad2f4b71ca0c92e5f6b1199d709cb176

Total reclaimed space: 20.97MB

$ docker volume prune -a -f --filter "label!=keep"
Deleted Volumes:
oldvol

Total reclaimed space: 31.46MB

$ docker volume ls
DRIVER    VOLUME NAME
local     pgbackup
local     redisdata

Note the first command: dangling=true lists any volume not attached to a container, named ones included. A database volume whose stack you stopped with docker compose down shows up there exactly like a forgotten test volume. “Dangling” is not a synonym for “safe to delete”.

Before volume prune -a, look inside what you are about to delete:

docker volume inspect oldvol --format '{{.Mountpoint}} {{.Labels}}'
docker run --rm -v oldvol:/v:ro alpine ls -la /v
docker run --rm -v oldvol:/v:ro -v "$PWD":/backup alpine tar czf /backup/oldvol.tgz -C /v .

Removed volumes cannot be recovered. A keep label on every volume that holds data, plus --filter "label!=keep" in scripts, costs nothing and protects against the worst mistake on this page.

Build cache: docker builder prune and buildx

With BuildKit (the default builder) the build cache is often the largest item on CI runners and developer machines. docker builder prune and docker buildx prune are the same command; docker buildx du shows the entries:

$ docker buildx du
ID                           RECLAIMABLE   SIZE      LAST ACCESSED
fv7huna25e5es2i13ey5x1ywd*   true          4.096kB   3 minutes ago
…
9kx1m9f3t0rumw7aftrk28nbr    true          12.09MB   10 minutes ago
0813zmd380hp4syglgdl3wye4    true          83.91MB   3 minutes ago
bubvhvi8ee547qu7z9seykk7b    true          104.9MB   10 minutes ago
Reclaimable:    201MB
Total:          201MB

Without -a the prune removes only cache that nothing references. While demo:latest still existed, that was almost nothing; with -a everything goes:

$ docker builder prune -f
…
Total:  24.68kB
$ docker builder prune -f --filter until=24h
Total:  0B
$ docker builder prune -a -f
Total:  117MB

Do not trust that last number blindly. buildx du had reported Shared: 117MB: with the containerd store the cache shares content with the image. Measured on disk, the same prune freed 58 MB, not 117:

$ sudo du -sm /var/lib/docker/containerd
223     /var/lib/docker/containerd
$ docker builder prune -a -f
Total:  117MB
$ sudo du -sm /var/lib/docker/containerd
165     /var/lib/docker/containerd

On a CI runner you usually want to keep some cache, not wipe it. buildx 0.38 offers size-based limits (older guides use --keep-storage, which no longer appears in the 0.38 help; the options are these):

docker builder prune -f --max-used-space 10gb     # trim the cache down to 10 GB
docker builder prune -f --min-free-space 20gb     # prune until 20 GB are free on disk
docker builder prune -f --filter until=72h        # entries not used in the last 3 days

See the docker buildx prune reference for the full option list. If you use a separate builder (docker buildx create, docker-container driver), its cache lives in that builder’s container: prune it with --builder name, the default prune does not reach it.

Container logs: clear them safely and stop them from growing

There is no docker logs prune or docker logs --clear. The default logging driver, json-file, writes to /var/lib/docker/containers/<id>/<id>-json.log with no limit: according to the json-file documentation, max-size defaults to -1 (unlimited) and max-file to 1.

Clear a log now: truncate, never rm

$ L=$(docker inspect -f '{{.LogPath}}' chatty)
$ ls -lh "$L" | awk '{print $5}'
313.3M
$ sudo truncate -s 0 "$L"
$ ls -lh "$L" | awk '{print $5}'
0

On a container that keeps writing, logging resumes in the same file and docker logs works normally (after a truncate of a “ticker” container, the file was 230 bytes three seconds later and docker logs printed the new lines). Two side effects: the history is gone, and with an empty file docker logs --tail 2 chatty hung until I killed it, while docker logs chatty returned immediately with no output. It started working again with the first new line.

Now what happens with rm instead, on a container writing continuously:

$ sudo rm "$(docker inspect -f '{{.LogPath}}' writer)"
$ sudo lsof -nP +L1 | grep json.log
dockerd  54 root 54w   REG  253,0 531214868     0 32685287 /var/lib/docker/containers/a6604ca8…-json.log (deleted)
$ docker logs --tail 1 writer
error from daemon in stream: Error grabbing logs: open /var/lib/docker/containers/a6604ca8…-json.log: no such file or directory

dockerd keeps the deleted file open and keeps writing to it: a few seconds later lsof reported 929 MB for a file that no longer exists in the directory. The disk does not get any space back, du no longer sees the file, and docker logs breaks. dockerd released the file only when I removed the container. If you already did this on a server, lsof +L1 shows the culprit; recreating (or restarting) the container makes dockerd close it.

Limit logs for every new container: daemon.json

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

Values in log-opts must be strings. Validate the file before restarting the daemon, because a wrong type stops dockerd from starting:

$ sudo dockerd --validate --config-file /tmp/bad.json      # "max-file": 3, without quotes
unable to configure the Docker daemon with file /tmp/bad.json: invalid JSON: json: cannot unmarshal number into Go struct field Config.CommonConfig.LogConfig.log-opts of type string
$ sudo dockerd --validate --config-file /etc/docker/daemon.json
configuration OK
$ sudo systemctl restart docker

Restarting the daemon stops the running containers unless "live-restore": true is set; plan it in a maintenance window. After the restart, the new limits applied only to the container created afterwards:

$ docker inspect -f '{{.Name}} {{.HostConfig.LogConfig.Type}} {{.HostConfig.LogConfig.Config}}' chatty chatty-new
/chatty json-file map[]
/chatty-new json-file map[max-file:3 max-size:10m]

$ ls -lh /var/lib/docker/containers/0eb835a1…/ | grep json
-rw-r-----  1 root root  8.2M  0eb835a1…-json.log
-rw-r-----  1 root root  9.5M  0eb835a1…-json.log.1
-rw-r-----  1 root root  9.5M  0eb835a1…-json.log.2

chatty, created before the change, was restarted (docker start) and wrote another 313 MB into its unlimited log. The log configuration is fixed when the container is created: restarting is not enough, the container must be recreated (docker compose up -d --force-recreate, or docker rm plus docker run). The documentation says it plainly: “Existing containers don’t use the new logging configuration automatically.”

Per container and in Compose

docker run -d --log-opt max-size=10m --log-opt max-file=3 nginx
services:
  web:
    image: nginx
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

I verified the Compose version: docker inspect on stack-web-1 showed map[max-file:3 max-size:10m]. Settings in the compose file override daemon.json for that service.

The local driver: rotation and compression by default

The local logging driver rotates out of the box (20 MB per file, 5 files, compressed rotated files). This time six million lines from a single container:

$ docker run -d --name chatty-local --log-driver local busybox:1.37 sh -c 'seq 1 6000000 | sed …'
$ ls -lh /var/lib/docker/containers/74d70f11…/local-logs
-rw-r-----  1 root root  16.6M  container.log
-rw-r-----  1 root root   1.7M  container.log.1.gz
-rw-r-----  1 root root   1.7M  container.log.2.gz
-rw-r-----  1 root root   1.7M  container.log.3.gz
-rw-r-----  1 root root   1.7M  container.log.4.gz
$ docker logs --tail 2 chatty-local
GET /api/v1/items status=200 id=4664512
GET /api/v1/items status=200 id=4664513

23 MB on disk where json-file without limits had 313 MB for half the lines. The catch: the format is internal, so tools that read *-json.log directly (Promtail, Filebeat, Fluent Bit tail inputs) will not work. If you ship logs that way, stay on json-file with limits. To use it everywhere, set "log-driver": "local" in daemon.json; the same rule applies, only new containers get it.

Logs written by the daemon itself go to the journal, not to these files: see journalctl commands for journalctl -u docker and journal size limits.

Docker Compose: there is no compose prune

Compose has no prune command. Its cleanup is down, and by default down keeps images and volumes:

$ docker compose down
 Container stack-web-1 Removed
 Network stack_default Removed
$ docker images --format '{{.Repository}}:{{.Tag}}' | grep stack
stack-web:latest
$ docker volume ls -q | grep stack
stack_webdata

$ docker compose down -v --rmi local
 Container stack-web-1 Removed
 Volume stack_webdata Removed
 Image stack-web:latest Removed
 Network stack_default Removed

--rmi local removes only images built by the project (no custom tag); --rmi all also removes pulled images such as postgres:16. -v deletes the named volumes declared in the file: on a stack with a database, that is the data. To clean up old images after a docker compose pull and up -d, the older versions become unused and docker image prune -a takes them.

A full prune, measured

End of the test: Redis running with its redisdata volume, the pgbackup volume unused, three stopped containers, an anonymous volume, a built image and its cache.

$ docker system prune -a --volumes -f
Deleted Containers:
df64960f45d6c32fb3346978ba5276b24e4d00c527efe987d6a00b0c0b27a9ff
…
Deleted Volumes:
eb5ee2dece5cd9678f39ecb368ff80b9e1e3e590acfe30770eea05be5538ec65

Deleted Images:
untagged: alpine:3.21
untagged: busybox:1.37
…
untagged: demo:latest
…
Deleted build cache objects:
tcqfiodsrnem1cl2jobgdezzo
…
Total reclaimed space: 228.7MB

$ docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          1         1         57.8MB    0B (0%)
Containers      1         1         4.096kB   0B (0%)
Local Volumes   2         1         89B       0B (0%)
Build Cache     0         0         0B        0B

$ docker volume ls
DRIVER    VOLUME NAME
local     pgbackup
local     redisdata

The running container, its image and both named volumes survived, including the unused pgbackup: --volumes took only the anonymous one.

Scheduled cleanup without surprises

A weekly job that does not touch data volumes and keeps recent images and some build cache:

#!/bin/sh
# /usr/local/bin/docker-cleanup.sh
set -eu
docker container prune -f --filter until=24h
docker image prune -a -f --filter until=168h --filter "label!=keep"
docker builder prune -f --max-used-space 10gb
docker volume prune -f            # anonymous volumes only
df -h /var/lib/docker

As a systemd timer instead of cron, so failures land in the journal:

# /etc/systemd/system/docker-cleanup.service
[Unit]
Description=Docker cleanup
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/docker-cleanup.sh

# /etc/systemd/system/docker-cleanup.timer
[Unit]
Description=Weekly Docker cleanup

[Timer]
OnCalendar=Sun 03:00
Persistent=true

[Install]
WantedBy=timers.target
sudo chmod +x /usr/local/bin/docker-cleanup.sh
sudo systemctl daemon-reload
sudo systemctl enable --now docker-cleanup.timer
systemctl list-timers docker-cleanup.timer

Two things kept out of the script on purpose: --volumes with -a, and log truncation. Logs are handled by the limits in daemon.json; a script that truncates logs weekly hides the containers that need fixing. For the logrotate approach to other files on the host, see logrotate: configure log rotation on Linux; do not point logrotate at Docker’s json logs while the daemon also rotates them.

Common errors

Symptom Cause Fix
df -h full, docker system df small Container logs, not counted by system df du on *-json.log, truncate, then limits
Space not freed after deleting a log dockerd still holds the deleted file lsof +L1, recreate or restart that container
docker image prune reclaims 0B No dangling images (containerd store) or layers shared with cache image prune -a, then builder prune
volume prune leaves named volumes Default since Engine 23: anonymous only volume prune -a, after checking contents
New log limits ignored Container created before the change Recreate it; a restart is not enough
Docker does not start after editing daemon.json Invalid JSON or number instead of string dockerd --validate --config-file …
--filter until=168h keeps old images The filter uses the local pull time Expected; filter by label for finer control
Reclaimed space larger than what df shows freed Shared build cache counted in full Measure with du / df, not the prune summary

If the space is not under /var/lib/docker at all, the generic method in free up Linux disk space with 12 commands finds it, including deleted files still held open.

Frequently Asked Questions

What is the difference between docker image prune and docker image prune -a?

Without -a it removes only dangling images, the untagged leftovers of rebuilds. With -a it removes every image that no container, running or stopped, was created from, including tagged images such as an old postgres version. Images used by containers are never removed.

Does docker system prune delete volumes?

Not by default. With the --volumes flag it also deletes anonymous volumes not used by any container. Named volumes are kept even then; only docker volume prune -a removes unused named volumes, and their data cannot be recovered.

How do I clear Docker container logs?

Find the log path with docker inspect, then empty the file with truncate -s 0. Do not delete it: Docker keeps writing to the deleted file and the disk space is not released. To prevent the problem, set max-size and max-file in daemon.json or use the local logging driver, then recreate the containers.

Does the log rotation in daemon.json apply to existing containers?

No. The logging configuration is fixed when a container is created, so existing containers keep unlimited logs even after the daemon and the container are restarted. You need to recreate them, for example with docker compose up -d --force-recreate.

Is it safe to run docker system prune -a on a production server?

It does not touch running containers, their images or any volume, so it will not stop services. The risk is elsewhere: images of stopped services are deleted and must be pulled again before they can start, which fails if the registry is unreachable or the tag has changed. Run it without -f first and read the list.

Sources

Tested on 1 November 2026: Docker Engine 29.9.2, containerd 2.4.2, buildx 0.38.1, Docker Compose 5.6.1.

Share this article:

Written by

Rosario Giordano

Rosario Giordano is a system administrator and IT consultant specializing in cybersecurity and cloud, with over 20 years of experience managing enterprise Linux infrastructures. His areas of expertise include SSH hardening, Kubernetes platforms, PostgreSQL databases, VMware/ Proxmox virtualization, and compliance with NIS2 and ISO 27001 security frameworks