Docker Remove Containers and Images

2018-07-30 Moduatedocker

Dockers CaaS (Containers as a Service) model allows developers to quickly build, ship and run their applications anywhere. Over time, however, you can end up with a large number of unused containers and images, which results in a cluttered environment.

Thankfully the Docker CLI (command line interface) has all the necessary commands for removing obsolete docker containers and images. This guide, will provide you with the necessary commands for cleaning up your environment and relinquish much needed disk space.

Docker Remove Container

To List your running containers

docker ps

You can list all of your containers by passing the -a option to the docker ps command (this includes stopped containers).

docker ps -a

To stop a container, pass in a containers name or id to the docker stop command.

docker stop <container_name>

To remove a container you pass its container name or id to the docker rm command. To remove more than one container, add the additional container names or ids to the first. Separate each with a space. Below, we demonstrate how to remove two containers. NB: The angle brackets should not be included with the container name/id.

docker rm <container_name_or_id> <container_name_or_id>

To automatically remove a container when it exits, simply add the —rm option to the docker run command.

docker run --rm <image_name>

Docker Remove All Containers

To stop all containers, you can pass $(docker ps -aq) to the docker stop command. The $() (linux command substitution), will invoke everything within the brackets inside a subshell, and append the resulting output to the original command. The docker ps -aq command that will be executed within the subshell will show all container IDs.

docker stop $(docker ps -aq)
  • The -a option shows all Containers (excluding -a would only show the running containers).
  • The -q (quiet) option only displays the containers numeric IDs.

If you would like to learn more about Linux command substitution, you can do so here.

Removing all containers is very similar to stopping all containers. Just replace the stop command with rm.

docker rm $(docker ps -aq)

To learn more about the different options for the docker stop commands you can check out the documentation as follows:

man docker ps
// or
docker ps --help

To check the documentation for the remove commands:

man docker rm
// or
docker rm --help

Docker Remove Image

To show images

docker images

To show all images (including intermediate images) pass in -a to the docker images command

docker images -a

To remove a image you pass its image name or id to the docker rmi command, or to remove multiple images, you can pass the additional image names or ids into the docker rmi command, separating each one with a space. Below, we demonstrate how to remove two images. NB: Do not include the angle brackets with the image name or id.

docker rmi <image_name_or_id> <image_name_or_id>

Docker Remove All Images

To remove all images, you can pass $(docker images -aq) to the docker rmi command. The $() (linux command substitution) will run whatever is within the brackets in a subshell. In this case it will run docker images -aq in a subshell and append the resulting image IDs to the original docker rmi command.

docker rmi -f $(docker images -aq)
  • The -f option of the rmi command will forcefully remove the image.
  • The -a option of the images command, returns all images.
  • The -q (quiet) option of the images command will only display numeric IDs.

To learn more about the different options for the docker rmi command you can check out the documentation as follows:

man docker rmi
docker rmi --help

Remove stopped containers and unused Images

To remove stopped containers and all Images no longer in use, run the following command.

docker system prune -a


This guide covers commonly used docker commands for removing containers and images from your environment. With these commands you will be able to clean up redundant or obsolete containers and images from the command line with very little effort.

If there is something you think should be added to this guide, please let us know in the comment section down below.