Log inSign up

Reclaim disk space from Docker without losing data

Start with docker system df to see how much space images, containers, local volumes and build cache take. Then remove only the type that is large: docker image prune -a for old images, docker builder prune for build cache, docker container prune for stopped containers. Leave volumes for last and check them by hand, because pruning a volume deletes data for good.

Topic: Containers and PaaS · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

How do you see what Docker is using?

The docker system df command prints a four-line summary with total size and how much of it is reclaimable. The verbose form lists every image, container and volume with its size:

df -h /
docker system df
docker system df -v | less
sudo du -sh /var/lib/docker

A typical summary has rows for Images, Containers, Local Volumes and Build Cache. The RECLAIMABLE column estimates what a prune of that type would free. Container log files are not counted in that table, so check them as well, as explained in the Docker logs guide. For the rest of the disk outside Docker, see the disk usage guide.

Which items are safe to remove?

Some Docker objects can be rebuilt or downloaded again at any time, others hold your only copy of data. Sort them before you delete anything:

  • Stopped containers: safe if you do not need their filesystem changes or logs. Anything important should already live in a volume.
  • Dangling images (shown as <none>): leftovers from rebuilds and updates, always safe.
  • Unused tagged images: safe, but they will be downloaded again the next time a container needs them.
  • Build cache: safe; the next build is just slower.
  • Unused networks: safe and tiny.
  • Volumes: may hold databases and uploads. Never prune these without checking each one.

How do you remove stopped containers and old images?

Prune stopped containers first, because an image cannot be removed while any container, even a stopped one, still refers to it:

docker ps -a --filter status=exited
docker container prune
docker image prune          # dangling images only
docker image prune -a       # every image not used by a container

Each command asks for confirmation and finishes with the total space freed. Add -f only in scripts where you have already tested the result. To keep recent images around for quick rollbacks, add an age filter, for example docker image prune -a --filter "until=168h", which spares anything created in the last seven days.

How do you clear the build cache?

Servers that build images, for example a CI runner, collect a lot of BuildKit cache. Clear it completely or keep a recent part:

docker builder du | tail -n 3
docker builder prune
docker builder prune --filter "until=72h"
docker builder prune -a

The plain form removes dangling cache only; -a clears all of it. When you use a separate buildx builder, prune it with docker buildx prune --builder NAME. Building is covered in the Docker images guide.

How do you deal with volumes carefully?

Look at unused volumes one by one, find out which project they belonged to, and only then delete:

docker volume ls -f dangling=true
docker volume inspect notes_dbdata --format '{{.Mountpoint}} {{.Labels}}'
sudo du -sh /var/lib/docker/volumes/*/_data | sort -h
docker volume rm notes_dbdata

Compose labels its volumes with the project name, which helps you recognise them. On Docker Engine 23 and newer, docker volume prune deletes only anonymous volumes unless you add --all. A volume that is unused because the container is stopped for maintenance still counts as dangling, so take a backup first; the volumes guide shows how.

What does docker system prune do?

It combines several prunes in one step: stopped containers, unused networks, dangling images and dangling build cache. With -a it also removes every unused image and all build cache. Volumes are left alone unless you pass --volumes:

docker system prune
docker system prune -a
docker system prune -a --filter "until=240h"

Read the warning list it prints before you type y. Avoid adding --volumes as a habit, since one forgotten stopped stack is enough to lose a database.

Can cleanup run on a schedule?

Yes, and a systemd timer is a tidy way to do it. Keep the scheduled job to images and build cache, which are always replaceable:

sudo tee /etc/systemd/system/docker-cleanup.service > /dev/null <<'EOF'
[Unit]
Description=Remove old Docker images and build cache
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
ExecStart=/usr/bin/docker image prune -af --filter until=168h
ExecStart=/usr/bin/docker builder prune -f --filter until=168h
EOF

sudo tee /etc/systemd/system/docker-cleanup.timer > /dev/null <<'EOF'
[Unit]
Description=Weekly Docker cleanup

[Timer]
OnCalendar=weekly
Persistent=true

[Install]
WantedBy=timers.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable --now docker-cleanup.timer
systemctl list-timers docker-cleanup.timer

Run it once by hand with sudo systemctl start docker-cleanup.service and read the result with journalctl -u docker-cleanup.service. More on timers is in the systemd services guide.

What should you never do to free space?

Do not delete folders inside /var/lib/docker by hand. Docker tracks layers and metadata in a database, and removing files behind its back leaves broken images and containers that refuse to start. If the disk is full and Docker will not even answer, stop the daemon, free space elsewhere (old logs, package caches with sudo apt clean or sudo dnf clean all), start it again and prune normally. If your data simply outgrows the disk, deploy a bigger plan and migrate as shown in the larger plan tutorial.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A2-4room for images and a few stacks2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months
S2-4larger SSD disk for many images2 vCPU, 4 GB RAM, 80 GB SSD$12.00/mo$72.00 for 6 months

Questions about Free disk space used by Docker

Will docker image prune -a break running containers?

No. Images in use by any container, running or stopped, are kept. Only images with no container at all are removed.

Why did the disk not shrink after pruning?

Large container logs, files inside running containers and named volumes are not touched by image or builder prunes. Check docker system df -v and the log files.

Is it safe to add --volumes to system prune?

Only when you have confirmed that no stopped stack holds data you need. A safer routine is to list dangling volumes and remove them by name.

How often should I clean up?

On a server that updates images weekly, a weekly prune of images and build cache older than a week keeps things tidy without removing rollback options.

Does pruning affect the backup add-on?

No. Backups are taken of the whole disk on their own schedule. Pruning only changes what is on the disk at the next backup.