Removing Docker containers and images

How to clean up unused images, containers, and free up disk space.

Docker is software for automating the deployment and management of applications using container-level virtualization. It lets you package an application with all its dependencies into an isolated container that can be moved to any Linux system with cgroups support.

Docker

For installation instructions, see Installing Docker on Ubuntu 18.04. Docker is also available on VPS and dedicated servers.

Full cleanup

To remove all stopped containers, unused networks, and dangling images in one go:

docker system prune

To also remove all images not associated with any container, add the -a flag:

docker system prune -a

Removing images

List all images:

docker images -a

Remove a specific image by name or ID:

docker rmi ubuntu

Remove dangling images — untagged layers with no associated containers that just take up space:

docker images -f dangling=true
docker image prune

Remove images by pattern — find matching images and pipe their IDs to docker rmi:

docker images -a | grep "centos" | awk '{print $3}' | xargs docker rmi

Remove all images:

docker rmi $(docker images -a -q)

Removing containers

List all containers:

docker ps -a

Remove a specific container by name or ID:

docker rm ID_or_Name

Auto-remove a container on exit — the --rm flag deletes the container as soon as it stops:

docker run --rm image_name

Remove all stopped containers:

docker rm $(docker ps -a -f status=exited -q)

Remove containers matching multiple filters — for example, with status exited or created:

docker rm $(docker ps -a -f status=exited -f status=created -q)

Remove containers by pattern:

docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rm

Stop and remove all containers:

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

Help

If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!

Need help?Our engineers will help you free of charge with any question in minutesContact us