Stop and remove all docker containers
Categories:
Mastering Docker: Stop and Remove All Containers

Learn essential commands and best practices to efficiently stop and remove all running and exited Docker containers, freeing up resources and maintaining a clean development environment.
Working with Docker often involves creating, running, and stopping numerous containers. Over time, these can accumulate, consuming valuable system resources and cluttering your environment. This article provides a comprehensive guide on how to effectively stop all running Docker containers and remove all containers, both running and exited, ensuring a clean and optimized Docker setup. We'll cover various scenarios and provide robust commands to manage your container lifecycle.
Understanding Docker Container States
Before we dive into stopping and removing containers, it's crucial to understand the different states a Docker container can be in. This knowledge helps in targeting specific containers for management. The primary states are:
- Running: The container is actively executing its process.
- Paused: The container's processes have been suspended.
- Exited: The container's main process has finished or been stopped. These containers still exist on your system and consume disk space.
- Restarting: The container is in the process of restarting (e.g., due to a restart policy).
When you stop a container, it transitions from 'running' to 'exited'. When you remove a container, it is completely deleted from your system.
stateDiagram-v2 [*] --> Created Created --> Running: docker start Running --> Paused: docker pause Paused --> Running: docker unpause Running --> Exited: docker stop / process ends Exited --> Running: docker start Exited --> Removed: docker rm Created --> Removed: docker rm Removed --> [*]
Docker Container Lifecycle States
Stopping All Running Containers
To stop all currently running Docker containers, you need to first identify them and then issue a stop command. This is a common task when you want to free up memory or prepare for system maintenance without immediately deleting the container's state.
docker stop $(docker ps -q)
Command to stop all running Docker containers
Let's break down this command:
docker ps -q
: This command lists all running containers (ps
) and outputs only their numeric IDs (-q
for quiet mode).docker stop
: This command stops one or more running containers.
By nesting docker ps -q
inside docker stop
, we effectively pass the IDs of all running containers to the stop
command, halting them gracefully.
docker kill $(docker ps -q)
. However, docker stop
is generally preferred as it sends a SIGTERM
signal, allowing the container to shut down gracefully, whereas docker kill
sends a SIGKILL
signal, immediately terminating the process.Removing All Containers (Running and Exited)
Once containers are stopped, they remain on your system in an 'Exited' state. To completely clean up your Docker environment, you'll want to remove these as well. This section covers how to remove all containers, including those that are currently running (which will be stopped first) and those that have already exited.
docker rm -f $(docker ps -aq)
Command to remove all Docker containers (running and exited)
Here's the breakdown:
docker ps -aq
: This command lists all containers (-a
for all, including stopped ones) and outputs only their numeric IDs (-q
).docker rm
: This command removes one or more containers.-f
(or--force
): This flag forces the removal of a running container. Without it,docker rm
would fail on running containers, requiring you to stop them first.
This single command is very powerful as it handles both stopping and removing in one go, making it ideal for a complete cleanup.
docker rm -f $(docker ps -aq)
. This command will delete ALL your containers, including any data volumes associated with them (unless the volume was explicitly created as a named volume and not removed with the container). Ensure you have backed up any critical data before executing this command.Alternative: Pruning All Unused Docker Objects
Docker provides a convenient prune
command to clean up various unused Docker objects, including containers, images, volumes, and networks. This is often the most comprehensive way to free up disk space.
docker system prune -a
Command to prune all unused Docker objects
The docker system prune
command is a powerful tool:
docker system prune
: Removes stopped containers, dangling images, and unused networks.-a
(or--all
): In addition to the above, it also removes all unused images (not just dangling ones) and all build cache.
This command will prompt you for confirmation before proceeding, giving you a chance to review what will be removed.
flowchart TD A[Start Cleanup] --> B{Identify Running Containers} B --> C[Stop Running Containers] C --> D{Identify All Containers (Running & Exited)} D --> E[Remove All Containers] E --> F[Cleanup Complete] subgraph Alternative Comprehensive Cleanup G[Start Prune] --> H{Identify Unused Containers, Images, Volumes, Networks} H --> I[Remove All Unused Objects] I --> F end
Flowchart for Docker Container Cleanup Strategies
1. Step 1: List all containers to review
Before performing any mass removal, it's a good practice to list all containers (running and exited) to ensure you don't accidentally delete something important. Use docker ps -a
.
2. Step 2: Stop all running containers
If you only want to stop containers without immediately removing them, execute docker stop $(docker ps -q)
. This allows for a graceful shutdown.
3. Step 3: Remove all containers
To remove all containers, including those that are running (which will be stopped first) and those that have already exited, use docker rm -f $(docker ps -aq)
.
4. Step 4: Consider a full system prune
For a more thorough cleanup that includes images, volumes, and networks, consider docker system prune -a
. Remember to confirm the action when prompted.