Docker Remove Stopped Containers

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: November 30, 2023

The short answer

To remove (prune) all stopped Docker containers at once, you can use the following command:

Bash
$ docker container prune

If you want to remove these containers without being prompted for confirmation, you can use the -f flag (short for force):

Bash
$ docker container prune -f

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:

Bash
$ docker container prune --filter "until=10m"
 $ docker container prune --filter "until=2023-01-15"

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.

Bash
$ docker rm  …
 $ docker rm  …

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:

Bash
$ docker ps --filter "status=exited" --filter "status=dead" -q

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:

Bash
$ docker rm $(docker ps --filter "status=exited" --filter "status=dead" -q)

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
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Learning Docker (The Easy Way) Using LazyDocker & Warp

A concise guide to learning Docker using Lazydocker. Highlights Docker’s benefits and takes advantage of Warp's AI features for a quick setup.

Run SSH In Docker

Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.

Remove a Docker Image

Learn how to remove a Docker image locally, on a Docker registry, and on Artifactory.

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.

Set Docker Container Hostname

Learn how to set, change and match a docker container hostname.

How To Use An .env File In Docker Compose

Learn how define and pass environment variables to Docker containers using an .env file in Docker Compose.

Use An .env File In Docker

Learn how to write and use .env files in Docker to populate the environment of containers on startup.

Restart Docker Containers

Learn how to restart Docker containers automatically with restart policies and manually using the docker restart, docker start, docker stop and docker kill commands.

Run Bash Shell In Docker

Start an interactive shell in Docker container