How to remove stopped and unused container in Docker

Remove stopped and unused container in Docker with docker container prune

Post Date: February 6, 2021
Last Updated: November 26, 2026

Introduction

This guide explains how to remove stopped and unused container objects in Docker with the command line. Every time a container exits, Docker keeps it on disk so you can inspect its logs and restart it. On a busy server, hundreds of exited containers pile up and use disk space, so cleaning them up regularly keeps the host healthy.

The original guide gave the right idea but had commands that fail. The stop-all and remove-all examples passed docker container ls -a into another command, which feeds table text and headings to Docker instead of container IDs. This version fixes them with the -q flag. It also replaces docker system prune with the narrower docker container prune, because system prune removes much more than containers. A verification step and troubleshooting are new.

Warning: Before you remove stopped and unused container objects, check what is stopped. A removed container loses its logs and any data stored inside it. Data in named volumes stays, but anonymous volumes may be deleted with docker rm -v. Back up anything you need first, and never run cleanup commands on a production host you have not inspected.

Prerequisites

Before you remove stopped and unused container objects, make sure you have:

  • Docker Engine installed and running (check with docker version)
  • A user in the docker group, or sudo access
  • SSH or terminal access to the Docker host
  • A list of containers you must keep, such as databases and long-running services
  • A backup of any data that lives inside a container instead of a volume

Architecture

  • The Docker CLI is the docker command you type. It sends each request to the daemon
  • The Docker daemon (dockerd) creates, runs, stops and removes containers
  • A container is running while its main process is alive, and becomes exited (stopped) when that process ends
  • A stopped container keeps its writable layer and logs on disk until you remove it
  • Images, volumes and networks are separate objects. Removing a container does not remove its image or its named volumes

The workflow is to list, stop what you no longer need, remove the stopped containers, then verify the result. The image below shows the container lifecycle. For the official references, see the Docker documentation for docker container prune and docker system prune.

Steps to Remove Stopped and Unused Container Objects in Docker

1. List All Docker Containers

docker ps -a

The newer form does the same:

docker container ls -a

The STATUS column shows Up for running containers and Exited for stopped ones. A container with the status Created was never started. To list only the stopped ones, add a filter:

docker ps -a --filter "status=exited"

To see how much disk space containers, images and volumes use, run:

docker system df

2. Stop a Running Container

Stop one container by its ID or name. The original guide said the ID is numeric, but it is a hexadecimal string, and you can use the name instead:

docker container stop my-container
docker container stop 3f2a9c1b7d4e

To stop every running container, use the -q flag so that only IDs are passed:

docker container stop $(docker container ls -q)

Note: The original used docker container ls -a here, which prints table headings and fails. Use -q for IDs only. Without -a, it lists only running containers, which is what you want when you stop containers.

3. Remove a Single Stopped Container

docker container rm my-container

To also delete the anonymous volumes that belong to that container, add -v:

docker container rm -v my-container

Docker refuses to remove a running container, which protects you from mistakes. Stop it first, or add -f only if you are sure.

4. Remove Stopped and Unused Container Objects in One Command

This is the main step to remove stopped and unused container objects. The safest command is docker container prune, which deletes every stopped container and asks for confirmation:

docker container prune

To skip the confirmation prompt in a script, add -f:

docker container prune -f

To remove only containers that stopped more than 24 hours ago, add a filter:

docker container prune --filter "until=24h"

An alternative is to remove containers by status:

docker container rm $(docker container ls -aq --filter "status=exited")

The original used docker container rm $(docker container ls -a) here, which does not work. The -q flag and the status=exited filter fix it.

5. Understand What docker system prune Removes

The original guide offered docker system prune as an alternative, but it does much more than clean up containers. By default it removes:

  • all stopped containers
  • all networks not used by a container
  • all dangling images (images with no tag)
  • the build cache

Adding -a also removes every image not used by a container, and adding --volumes also removes unused volumes, which can delete data. Use it only when you want a full cleanup:

docker system prune

Warning: docker system prune -a --volumes can delete database data stored in unused volumes. Run docker volume ls and docker image ls first, and prefer docker container prune when you only want to remove stopped and unused container objects.

6. Verify the Cleanup

docker ps -a
docker system df

Only the containers you want to keep should remain, and docker ps -a --filter "status=exited" should return no rows. The RECLAIMABLE column in docker system df shows how much space is still unused by images and volumes.

To avoid the buildup in the future, start short-lived containers with --rm, so Docker deletes them as soon as they exit:

docker run --rm alpine echo "hello"

Troubleshooting

If you cannot remove stopped and unused container objects, match the message you see:

  • “Error response from daemon: You cannot remove a running container … Stop the container before attempting removal or force remove”: The container is still running. Stop it first with docker container stop, then remove it
  • “permission denied while trying to connect to the Docker daemon socket”: Your user cannot reach Docker. Run the command with sudo, or add your user to the docker group and log in again
  • “Error response from daemon: No such container”: The ID or name has a typo, or the container was already removed. Run docker ps -a again
  • “docker container stop” requires at least 1 argument: The $(docker container ls -q) list is empty, because no container is running. This is harmless
  • “conflict: unable to delete … image is being used by stopped container”: You tried to remove an image while a stopped container still uses it. Remove the container first, then the image
  • Containers come back after you remove them: A restart policy, Docker Compose or an orchestrator is recreating them. Stop the Compose project with docker compose down, or change the policy with docker update --restart=no
  • The disk is still full after you remove stopped and unused container objects: The space is probably in images, volumes or the build cache. Run docker system df, then prune images with docker image prune
  • “Error response from daemon: removal of container … is already in progress”: Docker is already deleting it. Wait a moment and run docker ps -a again

Conclusion

To remove stopped and unused container objects in Docker, list them with docker ps -a, stop what you no longer need, and run docker container prune. Use docker container rm for a single container, and use -q to pass only IDs into other commands. Choose docker container prune over docker system prune unless you want to remove images, networks and the build cache as well. Verify with docker system df, and use --rm for short-lived containers.

Frequently Asked Questions

What is the safest way to remove stopped and unused container objects?
Run docker container prune. It removes only stopped containers, asks for confirmation, and leaves images, networks and volumes alone.

What is the difference between docker container prune and docker system prune?
container prune removes stopped containers only. system prune also removes unused networks, dangling images and the build cache, and with extra flags it removes all unused images and volumes.

Does removing a container delete its data?
It deletes the container’s writable layer and logs. Data in named volumes stays. Anonymous volumes are removed only if you use docker rm -v.

How do I remove all stopped containers except one?
Use a name filter in a list, for example docker container ls -aq --filter "status=exited", and remove the IDs you want. Or stop the one you want to keep from being exited by restarting it first.

Can I remove a container that is running?
Not without -f. Stop it first with docker container stop, which lets the process shut down cleanly.

How to Configure Docker Networking Between Containers

How to Configure Docker Container Restart Policies

Talk to DevOps and Cloud Experts

Unused containers and images quietly consume disk space and can slow down servers or cause deployment failures. At Pheonix Solutions, our team helps businesses manage container environments, automate cleanup tasks, configure servers, and maintain reliable application infrastructure.

We can help you with:

Need help managing Docker containers, cleaning up unused resources, or maintaining your container infrastructure? Talk to our DevOps and Cloud experts to discuss your requirements.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top