How to Clean Up Old Docker Images and Free Disk Space on Your VPS

How to Clean Up Old Docker Images and Free Disk Space on Your VPS

Docker makes it easy to ship apps, and it also makes it easy to fill a VPS disk without noticing. Every image you pull, every build you run, and every container you stop leaves files behind. A small VPS with a 40 GB disk can go from comfortable to full in a few months of regular updates. When you clean up old Docker images on a regular schedule, you get that space back before it turns into a 2 a.m. outage. This guide walks through the commands, in order, with the checks that keep your running containers safe.

Start by Seeing What Is Eating Your Disk

Do not delete anything before you know what is using the space. Run this first:

docker system df

The output breaks disk use into images, containers, local volumes, and build cache, with a reclaimable column for each. On a typical VPS that has run for months, images take the biggest share, followed by build cache. Next, check the disk itself:

df -h /

If the root filesystem sits above 85 percent, treat it as urgent. MySQL and other databases stop writing when the disk fills, and a full disk can stop Docker from starting new containers at all. A full disk is often the real cause behind a site that suddenly returns 502 errors, so it pays to check disk use the same week you notice deploys getting slow. If you want a routine for watching this number before it becomes a problem, read our guide on how to monitor VPS disk space before it fills up and crashes MySQL.

How to Clean Up Old Docker Images Without Breaking Running Containers

The fear that stops most people is deleting an image a live container still needs. Docker already protects you here: docker image prune only removes dangling images by default, which are untagged layers no container references. Running containers are never touched by a prune. The risk comes from the -a flag, which removes every image not linked to a container, including images you may want to roll back to. Keep the two modes apart and you stay safe.

Before any prune, list what you have:

docker images
docker ps -a

Compare the two lists. Any image with a container in the second list is in use. Images with no container and no tag are dangling, and those are the first to go. If you run several sites from one VPS, note which image each site uses before you touch anything, because an image name alone does not always show which project pulled it.

Remove Dangling Images First, Then Unused Ones

Start with the safe command. It removes dangling images only and asks for confirmation:

docker image prune

On a VPS that has seen a few months of updates, this often frees a few hundred megabytes to a few gigabytes. Check the result with docker system df again.

When you are ready to go further, remove all images not linked to a container:

docker image prune -a

Use this only after you confirm two things: every container you care about is running, and you do not need a quick rollback to an older image version. If you keep a previous image on purpose so you can roll back a bad deploy, pin it with a tag like myapp:previous before you prune. An image with a tag and no running container still gets removed by -a, so the tag alone does not protect it unless a container references it. For a safer update flow that keeps the old image around until the new one proves itself, see how to update Docker containers without downtime.

Clean Up Volumes and Build Cache Too

Images are only part of the story. Anonymous volumes left behind by deleted containers and old build cache can hold gigabytes. Check them with docker system df -v if you want the detail per volume.

Remove unused volumes with:

docker volume prune

Read the warning before you confirm. docker volume prune deletes every volume not attached to a container, and that includes data volumes from containers you stopped but plan to start again. If a stopped container holds a database you still need, start it first or back the volume up before you prune. Named volumes attached to running containers are safe, but an anonymous volume from a container you already removed is gone for good once pruned. Our guide to back up Docker volumes automatically shows a simple routine to run before any prune.

Build cache is the other silent grower. Every docker build leaves layers in the cache, and repeated builds of the same app stack them up. Clear it with:

docker builder prune

This only slows the next build a little, because Docker rebuilds the layers it needs. It never touches images or volumes.

When you want one command that covers images, containers, networks, and build cache together, use:

docker system prune

Add -a to include all unused images and --volumes to include volumes, but only after the backup check above. Run docker system prune without flags first and look at how much it reclaims. Most VPS owners find the plain version is enough for a monthly routine.

Automate the Cleanup So Disk Space Stays Free

A manual prune fixes today. A scheduled one stops the problem from coming back. The simplest way to reclaim Docker disk space on a schedule is a weekly cron job that runs the safe commands only:

0 3 * * 0 docker image prune -f > /var/log/docker-prune.log 2>&1
0 3 * * 0 docker builder prune -f >> /var/log/docker-prune.log 2>&1

The -f flag skips the confirmation prompt so cron can run it without input. Keep volumes out of the automated job. Volume pruning is the one step where a wrong guess deletes data, so run it by hand when you have checked what will go. Log the output so you can see how much space each run reclaims, and set an alert when disk use passes 80 percent so the schedule gets a look before the disk fills.

Log files deserve the same attention. Docker container logs grow without a limit unless you set one, and on a busy VPS they can quietly take more space than the images. Our guide on Docker log management to stop logs filling your disk shows how to cap log size per container.

What to Do When the Disk Is Already Full

If the disk is already at 100 percent, start with the commands that need no extra space. docker image prune and docker builder prune free space right away and do not need room to work. If Docker itself refuses to start, free a little room first by clearing old logs in /var/log or old package caches with apt clean, then run the prune.

Work through this order: dangling images, build cache, stopped containers, then unused volumes last, with a backup first. After each step, run df -h / and note the number. When the disk drops below 80 percent, restart the containers you stopped and confirm each site answers. Then put the weekly prune in cron so you do not repeat the same rescue next month. A VPS that gets a ten-minute cleanup each week rarely needs an emergency one.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.