SoFunction
Updated on 2025-03-10

Detailed steps and precautions for Docker deleting images

Docker Delete Images is a common operation to clean up images that are no longer needed to save storage space. Here are the detailed steps and notes about how to delete images by Docker:

1. View the current image

Before performing the deletion operation, you first need to view the mirror in the system to ensure that the required mirror will not be deleted accidentally. All images can be listed using the following command:

docker images

This command lists all images that have been downloaded to the local system, including the image name, version number (label), image ID, creation time and size, etc.

2. Delete a single image

  • Specify the image name or ID

    usedocker rmiThe command adds the name or ID of the image to delete a single image. For example, to delete the namemy_imageThe image can run:

    docker rmi my_image

    Or, if you know the image's ID (e.g.c3f279d17e0a), you can also delete it like this:

    docker rmi c3f279d17e0a
  • Forced deletion

    If the image is being used by one or more containers, Docker does not allow the image to be deleted by default. At this point, you can add-for--forceOption to force delete the image even if there is a container that is using it. For example:

    docker rmi -f my_image

3. Delete multiple images

To delete multiple images at once, you candocker rmiSpecify multiple mirror names or IDs in the command, separated by spaces. For example, to delete the nameimage1image2andimage3Three images can run:

docker rmi image1 image2 image3

Similarly, if you need to force delete these images, you can add-fOptions.

4. Delete all images

  • Delete all unused images

    usedocker image pruneThe command can delete all images that are not used by any container. This is a relatively safe operation because it does not delete the image being used.

    docker image prune

    Note: This command will prompt you to confirm whether to delete it, you can enteryCome and confirm.

  • Delete all images (including those in use)

    If you do need to delete all images, including those that are being used, you can use the following command combination:

    docker rmi $(docker images -q)

    here,docker images -qThe command will list the IDs of all images (without other information) and then pass it todocker rmiThe command is deleted. But please note that this is an irreversible operation and should be used with caution.

5. Things to note

  • Before deleting the image, make sure that the image is no longer needed to avoid accidentally deleting necessary images in the system.
  • If the image is being used by containers, you need to stop and delete the containers first, or force the image to be deleted (using-fOption).
  • docker image rmCommands anddocker rmiCommands are equivalent and can be used to delete images.
  • Deleting a mirror will only affect the local environment and will not affect the mirror in the remote repository. If you need to delete the mirror from the remote repository, you need to use the corresponding commands or operation interface provided by the remote repository.

Summarize

This is the article about the detailed steps and precautions for Docker deleting images. For more related content on Docker deleting images, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!