Marcio Cunha

How to clean Docker cache and free up disk space

Learn the practical step-by-step procedure to remove stopped containers, dangling images, and unused volumes in Docker using system prune.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • Stopped containers continue to consume static data and metadata resources on the host file system.
  • Dangling or untagged images accumulate dozens of gigabytes after successive application builds.
  • Unused volumes persist data on disk even after the complete removal of their associated containers.
  • The system prune command acts as a core deep-cleaning tool to wipe inactive resources all at once.
  • Periodic maintenance routines prevent disk overflow and unexpected interruptions in production environments.

Why does Docker consume so much disk space?

Anyone working with containers on a daily basis quickly notices that internal computer or server storage starts to shrink. In practice, this means the Docker engine stores every code layer, dependency, and temporary file generated during image builds. Over time, hundreds of megabytes or even gigabytes accumulate invisibly, requiring preventive maintenance similar to the routine of how to clear journalctl logs and free up disk space to restore breathing room to the operating system.

The main villain of this story isn't just the container currently running actively, but rather the buildup of artifacts that are no longer useful. When you update an image or recompile code, the previous version isn't automatically deleted. It remains stored on disk just in case it needs to be reused in a quick rollback process, turning into digital clutter if nobody performs periodic cleanups.

To understand exactly how much space your environment is wasting, the first logical step is to check current storage usage via the terminal. This inspection avoids unpleasant surprises and helps gauge the real impact before running any destructive cleaning command, functioning much like a preventive check in how to test RAM memory with MemTest86 step by step to diagnose hidden faults.

How to check current storage usage

Before deleting data randomly, we need to use an official tool that details what is consuming your disk. In practice, Docker's inspection command reveals separate tables for containers, images, local volumes, and build cache, allowing you to make an informed decision about what actually needs to be discarded.

Open your terminal and run the instruction below to list the general overview of storage consumption:

docker system df

This command displays the total size of each category, the space occupied by active items, and the slice that is idle. Analyzing these numbers is crucial to identifying whether the main problem lies in stopped containers, accumulated old images, or orphan volumes left behind after destroying old applications.

How to safely remove stopped containers

Containers that have finished execution still exist on disk along with their configuration files and internal state. If you have dozens of old instances that ran only for quick tests, they represent dead weight in your file structure.

To eliminate all currently stopped containers, execute the dedicated command for cleaning inactive instances:

docker container prune

The terminal will prompt for confirmation before proceeding with permanent deletion. Upon confirming, Docker instantly removes all inactive instances, freeing up the disk blocks associated with them and ensuring only what is strictly necessary keeps taking up server space.

How to clean dangling and untagged images

Container images are built in successive layers. When a new version is pulled, the old one loses its primary reference and turns into what we call a dangling image. In practice, they are loose pieces that do not belong to any running application.

To sweep and delete these images that take up precious space without real utility, use the following command in the terminal:

docker image prune

If you want to go further and remove absolutely all images not currently used by any active container (not just dangling ones), simply add the total exclusion flag:

docker image prune -a

This more aggressive option sweeps the local repository and discards any image not tied to a running container at that exact moment, usually returning a massive amount of gigabytes to the hard drive.

How to delete unused volumes

Docker volumes persist important data, such as databases and user-uploaded files, ensuring information isn't lost when a container is destroyed. The problem is that when we delete an entire project, we often forget to remove its associated volume.

These orphan volumes linger on disk indefinitely. To perform a safe sweep and erase all volumes not connected to any active container, type:

docker volume prune

Note that this operation is irreversible for data contained within these volumes. Therefore, make sure no critical production data resides in an abandoned volume before confirming deletion at the command prompt.

How to use system prune to clean everything at once

If you prefer not to run separate commands for containers, images, and volumes, Docker provides a master shortcut that automates all this cleanup in a single practical instruction. This is the universal system cleaning command.

To perform a general cleanup removing all stopped containers, unused networks, dangling images, and build cache, run:

docker system prune

If you wish to include all unused images (not just dangling ones) in the general tidy-up, add the extended flag:

docker system prune -a

This complete routine solves ninety percent of out-of-space issues caused by development and local testing environments, restoring stability and breathing room to the hardware.

Final considerations

Keeping the container environment organized prevents unpleasant surprises with full servers or deployment failures due to lack of disk space. Integrating commands like system prune into periodic maintenance routines ensures Docker remains a lightweight, agile, and predictable tool in your daily workflow.

Adopting good cleanup practices and continuous storage monitoring preserves host health and extends disk lifespan, letting you focus on software development without worrying about infrastructure bottlenecks.