Docker – Script to Remove Running Containers

Following script could help remove all the running Docker containers. Save the script in a file namely, delContainers.sh. Change the permission using command such as “chmod u+x delContainers.sh”. Execute the script using the command such as ./delContainers.sh. And, that is it. it would help you delete all the containers.

function clearContainers () { 
  CONTAINER_IDS=$(docker ps -aq)
  if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" == " " ]; then
    echo "---- No containers available for deletion ----"
  else
    docker rm -f $CONTAINER_IDS
  fi
}
clearContainers
Ajitesh Kumar

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.
Posted in Dockers. Tagged with .