Operations 11 min read

How to Clean Up Docker Disk Space: Remove Unused Images, Containers, Volumes & Networks

This guide explains why Docker consumes disk space, describes the different resource types and their states, and provides practical commands and shell scripts to safely prune unused or dangling images, containers, volumes, and networks, helping developers keep their environments lean.

Linux Cloud Computing Practice
Linux Cloud Computing Practice
Linux Cloud Computing Practice
How to Clean Up Docker Disk Space: Remove Unused Images, Containers, Volumes & Networks

If you are a heavy Docker/Kubernetes user, you have probably encountered the "no space left on device" error.

Before diving into how to prevent Docker from consuming excessive disk space, let’s first look at the resources Docker uses:

Image: The image files for containers, usually built with a Dockerfile and docker build.

Container: An isolated process created with docker create or docker run, using Linux namespaces.

Volume: Data (files or directories) mounted from the host into a container via Docker volumes.

Network: Enables communication between containers; its behavior depends on the chosen network driver (bridge, host, overlay, macvlan, none, external plugin).

Each resource can be in one of three states:

used: Currently referenced by at least one container.

unused: Not referenced by any container.

dangling: Stale images that will never be used again, typically created when the same tag is rebuilt multiple times.

Docker determines "used" vs. "unused" simply by checking whether any container references the resource; otherwise it is considered unused. "Dangling" applies only to images that have lost their tags (shown as <none> in docker image ls) and will never be used unless manually removed.

To clean up these unused or dangling resources, Docker provides a set of docker prune commands:

# Remove all unused images, not just dangling ones</code>
<code>docker image prune -a</code>
<code># Remove dangling images</code>
<code>docker image prune</code>
<code>docker network prune</code>
<code>docker volume prune</code>
<code>docker container prune</code>
<code># Remove all unused containers, networks, images (both dangling and unreferenced), and optionally volumes</code>
<code>docker system prune

These commands can be scheduled with system cron jobs to keep production VMs or CI/CD runners clean.

In personal development environments, you may want to keep certain unused images for faster rebuilds. For example, if a developer uses docker-compose to launch many containers, indiscriminately running docker compose rm && docker image prune could delete images needed for the next run.

A better approach is to use a Makefile or shell script that removes only dangling images or images with specific tags after a build:

docker image prune</code>
<code># or</code>
<code>docker image ls | grep "<YOUR_TAG_NAME>" | awk '{print $3}' | xargs docker image rm

The following script demonstrates a flexible cleanup tool that can delete images or containers matching a keyword, with an optional force flag:

#!/bin/bash
force=false
while getopts ":f" opt; do
  case ${opt} in
    f) force=true ;;
    ?) echo "Invalid option: -${OPTARG}" 1>&2; exit 1 ;;
    :) echo "Option -${OPTARG} requires an argument." 1>&2; exit 1 ;;
  esac
done
shift $((OPTIND -1))
if [ $# -ne 2 ]; then
  echo "Usage: docker-clean [-f] <resource> <keyword>"
  exit 1
fi
resource=$1
keyword=$2
if [ "$force" = true ]; then
  force_args="-f"
else
  read -p "Are you sure you want to delete all $resource with $keyword? [y/N] " confirmation
  if [ "$confirmation" != "y" ] && [ "$confirmation" != "Y" ]; then
    echo "Operation cancelled."
    exit 0
  fi
fi
case $resource in
  "image")
    docker images | grep "$keyword" | awk '{print $3}' | xargs docker rmi $force_args
    ;;
  "container")
    docker ps -a | grep "$keyword" | awk '{print $1}' | xargs docker rm $force_args
    ;;
  *)
    echo "Invalid resource type. Must be either 'image' or 'container'."
    exit 1
    ;;
esac

Beyond pruning, you can improve disk usage by using slimmer base images, reusing base images when possible, avoiding excessive docker commit operations, and removing build‑time packages after image creation.

Finally, remember that over‑aggressive cleanup can delete cached layers that speed up subsequent builds, so tailor your cleanup strategy to your workflow.

Docker is a powerful tool for developers, but misuse can quickly fill up disk space; the techniques above help keep your environment efficient.

Docker resource diagram
Docker resource diagram
DockerContainer Managementshell scriptpruneDisk Cleanupunused images
Linux Cloud Computing Practice
Written by

Linux Cloud Computing Practice

Welcome to Linux Cloud Computing Practice. We offer high-quality articles on Linux, cloud computing, DevOps, networking and related topics. Dive in and start your Linux cloud computing journey!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.