keepcalculator

06 Numbers and Powers

Docker Container Cleanup Commands (Safe Guide)

Card No. 06·4519 · Updated · Standard formula, shown below

This guide walks you through the safe removal of Docker containers, covering the essential commands to clean up unused or stopped containers without accidentally deleting data you need. You’ll learn how to list containers, remove individual ones, and use the built-in cleanup commands, plus how to avoid common mistakes that can lead to lost volumes or images.

What You’ll Need

  • A working Docker installation (Docker Engine or Docker Desktop) with command-line access.
  • Basic familiarity with running commands in a terminal or command prompt.
  • Sufficient permissions to run Docker commands (usually your user is in the docker group, or you’re using sudo).

Step-by-Step

  1. List all containers – Run docker ps -a to see every container on your system, including stopped ones. The output shows container IDs, names, images, status, and ports. This is your starting point to decide what to remove.
  2. Remove a specific stopped container – Use docker rm <container_id_or_name>. For example, docker rm my_app or docker rm 3f2a1b. If the container is still running, you’ll get an error; you can either stop it first with docker stop <id> or use docker rm -f to force removal (use with caution).
  3. Remove a running container (force) – If you want to delete a container that is currently running, use docker rm -f <id>. This sends a SIGKILL signal and removes the container. Be aware that any unsaved data inside the container’s writable layer is lost.
  4. Remove all stopped containers – The command docker container prune removes all containers that are not running. It will ask for confirmation unless you add -f (force). This is a safe, convenient way to clean up many stopped containers at once.
  5. Remove containers and associated anonymous volumes – To delete stopped containers and the anonymous volumes attached to them, use docker container prune -a or docker system prune (which does more, see step 7). The -a flag on docker container prune is not standard; instead use docker system prune --volumes to include volumes. For container-only cleanup with volumes, you can manually remove volumes with docker volume prune after removing containers.
  6. Remove a container and its volumes – When you remove a container with docker rm -v <id>, Docker also removes any anonymous volumes attached to that container. Named volumes are not removed automatically, even with -v.
  7. Full system cleanup – Run docker system prune to remove stopped containers, unused networks, dangling images, and build cache. Add -a to also remove all unused images (not just dangling). Add --volumes to also remove unused volumes. This is a powerful command, so review what it will delete before confirming.
  8. Remove all containers (both running and stopped) – If you want to remove every container, first stop all running ones with docker stop $(docker ps -q), then remove all with docker rm $(docker ps -aq). This is a brute-force approach; use it only when you’re sure you don’t need any containers.

Common Mistakes

  • Forgetting to stop a container before removing – docker rm fails on a running container. Always stop it first or use -f.
  • Using docker system prune -a without understanding it – This removes all unused images, not just dangling ones. If you have images you want to keep (even if not used by a container), they will be deleted.
  • Ignoring volumes – Removing a container does not remove its named volumes by default. If you want to free up disk space, you must explicitly remove volumes with docker volume prune or docker rm -v.
  • Removing a container that is part of a compose stack – If you use Docker Compose, removing a container manually can leave the stack in an inconsistent state. Use docker compose down instead.
  • Using docker rm -f on a database container – Forcing removal can corrupt data if the container is writing to a volume. Always stop gracefully first if possible.

Tips

  • Use docker ps -a --filter status=exited to list only stopped containers, making it easier to see what can be cleaned.
  • Before a big cleanup, run docker ps -a and docker images to note what you might want to keep.
  • Use docker container prune -f to skip the confirmation prompt when you’re sure.
  • To see how much disk space you’ll free up, run docker system df before pruning.
  • If you use Docker Compose, always use docker compose down to remove containers, networks, and optionally volumes defined in the compose file.
  • Consider using docker system prune --filter "until=24h" to remove resources older than a day, keeping recent ones.

Troubleshooting

If a removal command fails, here are common issues and fixes:

  • “You cannot remove a running container” – Stop the container first with docker stop <id>, or use docker rm -f to force it.
  • “Error: No such container” – The container ID or name is wrong. Double-check with docker ps -a.
  • Permission denied – If you get a permission error, your user may not be in the docker group. Use sudo before the command, or add your user to the group (then log out and back in).
  • Volume still taking up space – After removing containers, run docker volume ls to see orphaned volumes and remove them with docker volume prune.
  • Image not removed after pruning – If an image is still used by a container (even a stopped one), it won’t be removed. Remove the container first.
  • Prune hangs or seems stuck – This can happen with many containers. Wait a bit; if it takes too long, check for a stuck container with docker ps -a and remove it manually.

Frequently Asked Questions

What is the difference between docker rm and docker container prune?

docker rm removes a specific container (by ID or name). docker container prune removes all stopped containers at once. Use docker rm for targeted removal, and prune for bulk cleanup.

Does removing a container delete its data?

It depends. The container’s writable layer is deleted, but data stored in volumes (especially named volumes) persists. Anonymous volumes are removed only if you use docker rm -v or a prune command with --volumes.

How do I remove all containers, including running ones?

Run docker rm -f $(docker ps -aq). This force-removes every container. Be careful: it will stop running containers abruptly.

Will docker system prune remove my images?

By default, it removes only dangling images (those with no tag). With the -a flag, it removes all unused images (not referenced by any container). It does not remove images that are in use.

How do I remove a container and its volumes together?

Use docker rm -v <id> to remove anonymous volumes. For named volumes, you must remove them separately with docker volume rm <volume_name> or use docker compose down -v if using Compose.

Is it safe to run docker system prune -a --volumes?

It is safe in the sense that it only removes resources not used by any running container. However, it will delete all unused images and volumes, which might include data you wanted to keep. Always review what it lists before confirming.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →