Skip to content

How to cleanup Docker

Docker doesn’t remove unused objects such as containers, images, volumes, and networks unless you explicitly tell it to do so. How To Remove them?

Remove stopped containers, all dangling images, and all unused networks:

Terminal window
docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N]

You can also include all unused volumes, then add --volumes:

Terminal window
docker system prune --volumes
Terminal window
docker container ls -a
Terminal window
docker container rm [CONTAINER ID]

To remove all stopped containers use:

Terminal window
docker container prune

You can also list what will be removed:

Terminal window
docker container ls -a --filter status=exited --filter status=created
Terminal window
docker container stop $(docker container ls -aq)
docker container rm $(docker container ls -aq)

You can list them:

Terminal window
docker image ls
Terminal window
docker image rm [IMAGE ID]

A dangling image is an image that is not tagged and is not used by any container. You can remove them by:

Terminal window
docker image prune

To remove all images which are not referenced by any existing container, not just the dangling ones, use the prune command with the -a flag:

Terminal window
docker image prune -a

You can list them with:

Terminal window
docker network ls
Terminal window
docker network rm [NETWORK ID]

Use the docker network prune command to remove all unused networks. Remove all networks that are created more than 12 hours ago:

Terminal window
docker network prune -a --filter "until=12h"
Terminal window
docker volume ls
Terminal window
docker volume rm [VOLUME NAME]
Terminal window
docker volume prune