Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete all running Docker containers?

Tags:

docker

I remember using

docker rm -f `docker ps -aq`

to chain the commands without an issue a few months ago, but now this isn't working, and I'm getting the following output:

unknown shorthand flag: 'a' in -aq`
See 'docker rm --help'.

What changed? How can I delete all Docker running containers in one line? In case it helps, I'm using Docker for Windows (native with Hyper-V, not with VirtualBox) on Windows 10, and the command I used has worked fine with my previous Windows 8 Docker toolbox installation.

like image 223
sgarcia.dev Avatar asked Jun 26 '16 17:06

sgarcia.dev


People also ask

How do I delete multiple docker containers?

To remove one or more Docker containers, use the docker container rm command, followed by the IDs of the containers you want to remove. If you get an error message similar to the one shown below, it means that the container is running. You'll need to stop the container before removing it.

How do I clean my docker?

To clean this up, you can use the docker container prune command. By default, you are prompted to continue. To bypass the prompt, use the -f or --force flag. Other filtering expressions are available.


3 Answers

Till now (Docker version 1.12) we are using the following command to delete all the running containers (also, if we want to delete the volumes, we can do that manually using its respective tag -v in the following command),

Delete all Exited Containers

docker rm $(docker ps -q -f status=exited)

Delete all Stopped Containers

docker rm $(docker ps -a -q)

Delete All Running and Stopped Containers

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

Remove all containers, without any criteria

docker container rm $(docker container ps -aq)

But, in version 1.13 and above, for complete system and cleanup, we can directly user the following command,

docker system prune

All unused containers, images, networks and volumes will get deleted. Also, individually i.e. separately, we can do that using the following commands, that clean up the components,

docker container prune
docker image prune
docker network prune
docker volume prune
like image 59
mohan08p Avatar answered Sep 17 '22 21:09

mohan08p


I had this issue when running in cmd. Switched to PowerShell and it worked!

like image 44
D-Go Avatar answered Sep 19 '22 21:09

D-Go


Use:

docker rm -f $(docker ps -aq)
like image 25
fazni Avatar answered Sep 19 '22 21:09

fazni