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.
npm run import -- "Delete images in cmd"
FOR /f "tokens=*" %i IN ('docker images -q') DO docker rmi %i
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:
FOR /f "tokens=*" %i IN ('docker images -q')
:
docker images -q
command.
FOR /f
is used to process text files, in this case, the output of the command."tokens=*"
specifies that all tokens (words) in each line of the output should be captured.%i
is a variable that will hold the current token (image ID) in each iteration.'docker images -q'
executes the command to list all Docker images and their IDs.DO docker rmi %i
:
%i
.
docker rmi %i
removes the Docker image with the ID stored in the %i
variable.In essence, this batch script efficiently iterates through all Docker images and removes them one by one.