docker commands | Delete containers in cmd | Delete danglings images in cmd | Search

This Windows batch script removes all Docker images by iterating through a list of image IDs obtained from docker images -q and using docker rmi to delete each one.

Run example

npm run import -- "Delete images in cmd"

Delete images in cmd

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

What the code could have been:

bash
#!/bin/bash

# Remove all unused Docker images
UNUSED_IMAGES=$(docker images -q --format '{{.ID}}' | grep -v '^<none>')  # Filter out <none> images

# Print the images to be removed
echo "The following images will be removed:"
echo "$UNUSED_IMAGES"

# Prompt for confirmation before removing the images
read -p "Are you sure you want to remove these images? (yes/no): " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
    docker rmi -f "$UNUSED_IMAGES"
    echo "Images removed successfully."
else
    echo "Image removal cancelled."
fi

# TODO: Consider implementing a filter to only remove images older than a certain age

This code snippet removes all Docker images from your system.

Here's a breakdown:

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

  2. DO docker rmi %i:

In essence, this batch script efficiently iterates through all Docker images and removes them one by one.