'How do I delete all running Docker containers?
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.
Solution 1:[1]
I had this issue when running in cmd. Switched to PowerShell and it worked!
Solution 2:[2]
Use:
docker rm -f $(docker ps -aq)
Solution 3:[3]
If anybody needs the Windows Shell Command (stop and remove container), here it is:
for /F %c in ('docker ps -a -q') do (docker stop %c)
for /F %c in ('docker ps -a -q') do (docker rm %c)
If you put it in a batch file, just add % to %c:
for /F %%c in ('docker ps -a -q') do (docker stop %%c)
Solution 4:[4]
I've had the same problem: I was on a Windows machine and used Docker within VirtualBox and the command docker rm -f ${docker ps -aq} worked well. Then I switched to Docker for Windows and the command didn't work on the Windows command line.
But using Cygwin or Git Bash solved the problem for me.
Solution 5:[5]
Try using this command.
docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')
Solution 6:[6]
Run docker commands in Windows PowerShell will execute and run most of the commands
Hope you also remember to stop running containers first before running delete command
docker stop $(docker ps -aq)
Solution 7:[7]
$ docker rm $(docker ps --filter status=created -q)
Tested on Docker version 19.03.5, build 633a0ea on Mac OS Mojave.
Solution 8:[8]
If the container is running, you cannot delete the image. First stop all the containers using the following command.
docker stop $(docker ps -aq)
you are saying running stop against the output of docker ps -aq. 'a' - get me all the containers 'q' - return only the container id.
Then run the following command to remove all the containers.
docker rm $(docker ps -aq)
Solution 9:[9]
Use this:
docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')
If you want to include previously stopped containers:
docker rm -f $(docker ps -a | grep -v CONTAINER | awk '{print $1}')
Solution 10:[10]
Single command to delete all stop and running containers (first stop and then just prune/remove them. Works for me all the time.
docker stop $(docker ps -a -q) && docker container prune -a
Solution 11:[11]
we can delete all running container in docker ENV by following command -
docker container rm -f $(docker container ls -aq)
It should to the magic
if we have run our docker container using docker-compose.yaml then
docker-compose -f /path/to/compose/file down
should work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
