docker commands | Delete images in cmd | Cell 9 | Search

This Windows batch script identifies and removes all dangling Docker images from your system.

Run example

npm run import -- "Delete danglings images in cmd"

Delete danglings images in cmd

FOR /f "tokens=*" %i IN ('docker images -q -f "dangling=true"') DO docker rmi %i

What the code could have been:

#!/bin/bash

# Function to remove dangling Docker images
remove_dangling_images() {
  # Get a list of dangling images using Docker
  dangling_images=$(docker images -q -f "dangling=true")

  # Iterate over the list using while loop for better readability
  while IFS= read -r image; do
    # Remove each dangling image
    docker rmi "${image}" || echo "Error removing image ${image}"
  done <<< "$dangling_images"
}

# Call the function to remove dangling images
remove_dangling_images

This code snippet removes all dangling Docker images from the system.

Breakdown:

  1. FOR /f "tokens=*" %i IN ('docker images -q -f "dangling=true"'):

  2. DO docker rmi %i:

In essence:

Here's a breakdown:

In summary: This code snippet finds all dangling Docker images on your system and then removes them.