2024-12-15
8 min read

Delete All Local Docker Images Safely

Delete All Local Docker Images Safely

Docker images accumulate quickly during development. A single Node.js project might pull base images, dependencies, and create multiple build layers. After a few months of active development, you might find Docker consuming 20GB or more of disk space. Here's how to safely clean up your local Docker environment without disrupting your workflow.

Prerequisites

Before starting this cleanup process, make sure you have:

  • Docker installed and running on your system
  • Administrative access to run Docker commands
  • A backup of any critical container data or custom images you want to preserve

Understanding Docker's Storage Structure

Docker stores images in layers, and multiple images can share the same base layers. When you see this structure:

Local Docker Storage
├── Images (base OS, dependencies, application code)
├── Containers (running instances of images)
├── Volumes (persistent data storage)
└── Networks (container communication)

Each component can be cleaned up independently, giving you fine-grained control over what to remove.

Check Your Current Docker Usage

Start by examining what Docker resources you're currently using. This helps you understand what you're working with and identify what's safe to remove.

# View disk usage breakdown by Docker component
docker system df

This command shows exactly how much space images, containers, and volumes are consuming:

TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          45        12        8.2GB     6.1GB (74%)
Containers      23        3         2.1GB     1.8GB (85%)
Local Volumes   8         2         1.2GB     800MB (66%)
Build Cache     0         0         0B        0B

Next, see what's currently running and what depends on your images:

# List all running containers with their image dependencies
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"

# Show all containers, including stopped ones
docker ps -a --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"

Selective Image Cleanup

Before removing everything, consider cleaning up selectively. This approach is safer and often sufficient.

Remove images that aren't associated with any containers:

# Remove unused images (safer option)
docker image prune

This command only removes "dangling" images - layers that aren't tagged and aren't referenced by any container. You'll see output like:

WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
sha256:abc123... (untagged)
sha256:def456... (untagged)
Total reclaimed space: 1.2GB

For a more aggressive cleanup that removes all unused images:

# Remove all images not currently used by containers
docker image prune -a

The -a flag removes all unused images, not just dangling ones. This includes tagged images that no containers are using.

Complete Image Removal

When you need a completely clean slate, remove all local images. This is useful when switching between different projects or when Docker's cache becomes corrupted.

First, stop all running containers to avoid conflicts:

# Stop all running containers gracefully
docker stop $(docker ps -q)

If containers don't stop gracefully, force them:

# Force stop stubborn containers
docker kill $(docker ps -q)

Now remove all containers and then all images:

# Remove all containers (stopped and running)
docker rm $(docker ps -aq)

# Remove all images forcefully
docker rmi $(docker images -q) -f

Alternatively, use the prune command with force flag to skip confirmations:

# Remove all images in one command
docker image prune -af

Clean Up Additional Resources

Images are just one part of Docker's storage footprint. Clean up other components for maximum space recovery.

Remove unused volumes (be careful with this - volumes contain persistent data):

# Remove only anonymous volumes not used by containers
docker volume prune

# Remove ALL unused volumes (including named ones)
docker volume prune -a

Clean up networks that containers no longer use:

# Remove unused networks
docker network prune

For a comprehensive cleanup of everything unused:

# Nuclear option: remove everything unused
docker system prune --volumes -f

This single command removes:

  • All stopped containers
  • All unused networks
  • All unused images
  • All unused volumes
  • All build cache

Verify Your Cleanup

After cleanup, verify that Docker is using minimal space and that your essential services still work:

# Check remaining Docker resources
docker system df

# List remaining images
docker images

# Test that Docker still works
docker run --rm hello-world

You should see significantly reduced disk usage. If you removed all images, Docker will need to re-download base images when you next run containers.

Preventing Future Bloat

Set up automatic cleanup to prevent Docker from consuming excessive disk space:

# Add this to your shell profile for weekly cleanup
alias docker-cleanup='docker system prune -f && docker volume prune -f'

Consider using multi-stage builds in your Dockerfiles to reduce final image size:

# Multi-stage build example
FROM node:18 AS builder
COPY package*.json ./
RUN npm ci --only=production

FROM node:18-alpine
COPY --from=builder /app/node_modules ./node_modules
COPY . .
CMD ["npm", "start"]

Recovery After Cleanup

If you accidentally removed important images, Docker will re-download them when needed. However, this takes time and bandwidth. Keep these recovery strategies in mind:

For development environments, maintain a docker-compose.yml file that defines your standard services:

version: '3.8'
services:
  app:
    build: .
    ports:
      - '3000:3000'
  database:
    image: postgres:15
    environment:
      POSTGRES_DB: myapp

Run docker-compose up to quickly restore your development environment.

For important custom images, push them to a registry before cleanup:

# Tag and push custom images before cleanup
docker tag my-custom-app:latest username/my-custom-app:latest
docker push username/my-custom-app:latest

Cleaning up Docker images regularly keeps your development environment fast and your disk space manageable. Start with selective cleanup using docker image prune, and only use the nuclear option when you need a completely fresh start.

Published: 2024-12-15|Last updated: 2024-12-15T09:00:00Z

Found an issue?