docker commands | Delete all containers | Delete all images in Powershell | Search

This code removes all Docker images by first listing their IDs and then using those IDs to target the docker rmi command.

Run example

npm run import -- "Delete all images"

Delete all images

docker rmi $(docker images -q)

What the code could have been:

bash
#!/bin/bash

# Function to remove unused Docker images
remove_unused_images() {
  # Get a list of all Docker images, except the ones with no size (i.e., running containers)
  # This is done using the -q flag to suppress the image ID and the --filter flag to exclude images with a size of 0
  local image_ids=$(docker images -f "dangling=false" -q)

  # Remove the images from the Docker registry
  # The -f flag is used to specify that we want to force the removal of the images
  docker rmi -f "$image_ids"
}

# Call the function to remove unused Docker images
remove_unused_images

This code snippet removes all Docker images from your system.

Here's a breakdown:

  1. docker images -q:

  2. $(...):

  3. docker rmi:

In essence, this command provides a concise way to clean up all Docker images on your system.