Skip to content

Instantly share code, notes, and snippets.

@ORESoftware ORESoftware/
Last active May 28, 2019

What would you like to do?
Cleanup docker artifacts/items

Clean up docker images/containers/volumes/networks

#!/usr/bin/env bash

set +e;

  while read line; do
    if test -z "$line"; then
        echo 'There was an empty line, exiting.' # > /dev/stderr
        exit 0;
    echo "$line"

export -f handle_empty;

# remove all unused / orphaned images
docker images --no-trunc | grep "<none>" | awk "{print \$3}" | handle_empty | xargs docker rmi -f | cat;

# clean up containers
docker ps --filter status=dead --filter status=exited -aq | handle_empty  | xargs docker rm -v | cat;

# clean up volumes
docker volume ls -qf dangling=true | handle_empty | xargs docker volume rm | cat;

# clean up networks
docker network ls --format "{{json . }}" | jq -r '.Name' | while read name; do
     if test "$name" == "interos-test"*; then
        docker network rm "$name" | cat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.