People that work with Docker know it is easy to create a large number of containers. Occasionally it becomes necessary to delete unused and unneeded containers. Below is an example of stopping and deleting all existing Docker containers. This is accomplished by using docker stop and docker rm together with docker ps (using command substitution).

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q) 

docker ps is used to list containers. The -a option will list all containers (not just running containers). The -q option will display only the container IDs. Usage of the docker ps command can be seen below:

Usage:	docker ps [OPTIONS]

List containers

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter value    Filter output based on conditions provided (default [])
      --format string   Pretty-print containers using a Go template
      --help            Print usage
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display numeric IDs
  -s, --size            Display total file sizes

Leave a Reply

How to Stop and Delete all Docker Containers