To safely cleanup Docker resources like images, containers, and disk space, you can follow the steps below:
  1. Stop and remove all running containers:

    docker stop $(docker ps -a -q) docker rm $(docker ps -a -q
  2. Delete all unused Docker images:

    docker image prune -a

    This command will remove all images that are not associated with a container or tag and are not being used by any other images.

  3. Remove all unused Docker volumes:

    docker volume prune

    This command will remove all volumes that are not associated with a container.

  4. Clear the Docker build cache:

    docker builder prune

    This command will remove all unused build cache data.

  5. Remove all unused Docker networks:

    docker network prune

    This command will remove all networks that are not associated with a container.

  6. Remove all stopped Docker containers: (Be careful)

    docker container prune

    This command will remove all containers that are not running.

  7. Check the amount of disk space used by Docker:

    docker system df

    This command will show you how much space is being used by Docker on your system.

  8. Finally, to reclaim disk space used by Docker, you can run: (Be careful)

    docker system prune -a --volumes

    This command will remove all unused resources, including containers, images, networks, and volumes.

Note: Be careful when running these commands, as they will remove all unused resources. Make sure you do not delete any resources that are still being used by your running containers.

Leave a Reply

Your email address will not be published. Required fields are marked *