Docker Remove Stopped Containers
Razvan Ludosanu
Founder, learnbackend.dev
Published: 11/30/2023
The short answer
To remove (prune) all stopped Docker containers at once, you can use the following command:
$ docker container prune
Run in Warp
If you want to remove these containers without being prompted for confirmation, you can use the -f flag (short for force):
$ docker container prune -f
Run in Warp
Use Warp's Workflows feature to easily recall this syntax
If you’re using Warp as your terminal and you need to quickly retrieve this command, you can use Warp's Workflows feature by pressing CTRL-SHIFT-R and typing remove stopped containers:
Then pressing ENTER to use the suggested command:
Filtering stopped containers by creation time
The docker container prune command offers the possibility to filter the containers that will be removed using the --filter flag.
To remove all stopped containers that were created before a certain time, you can use the until=<timestamp> filter, where timestamp can be a Unix timestamp, a date formatted timestamp, or Go duration string.
For example:
$ docker container prune --filter "until=10m"
$ docker container prune --filter "until=2023-01-15"
Run in Warp
Removing all stopped containers using docker rm
The docker rm command is used to remove one or more stopped containers using their name or ID.
$ docker rm …
$ docker rm …
Run in Warp
List and show the IDs of stopped containers with docker ps
To get a list of all stopped containers IDs – which are containers with a status equivalent to exited or dead – you can use the docker ps command combined with the --filter and the -q flags:
$ docker ps --filter "status=exited" --filter "status=dead" -q
Run in Warp
Where:
- The --filter flag is used to filter the list of containers returned by the docker ps command.
- The -q flag is used to return their IDs only.
Removing all stopped containers
To remove all stopped containers at once with docker rm, you can combine it with the previous docker ps command using the command substitution syntax:
$ docker rm $(docker ps --filter "status=exited" --filter "status=dead" -q)
Run in Warp
Where the expression contained in parenthesis $(expression) will be replaced by its result, which in this case will be the list of all stopped containers IDs.
Written by
Razvan Ludosanu
Founder, learnbackend.dev
Filed Under
Related Articles
Override the Container Entrypoint With docker run
Learn how to override and customize the entrypoint of a Docker container using the docker run command.
The Dockerfile ARG Instruction
Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.
Start a Docker Container
Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.
Stop All Docker Containers
How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.
Use An .env File In Docker
Learn how to write and use .env files in Docker to populate the environment of containers on startup.
Run SSH In Docker
Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.
Launch MySQL Using Docker Compose
Learn how to launch a MySQL container in Docker Compose.
Execute in a Docker Container
Learn how to execute one or multiple commands in a Docker container using the docker exec command.
Expose Docker Container Ports
Learn how to publish and expose Docker container ports using the docker run command and Dockerfiles.
Restart Containers In Docker Compose
Learn how to restart and rebuild one or more containers in Docker Compose.
Output Logs in Docker Compose
Learn how to output, monitor, customize and filter the logs of the containers related to one or more services in Docker Compose
Rename A Docker Image
Learn how to rename Docker images locally and remotely using the docker tag command.