Skip to content

Instantly share code, notes, and snippets.

@rcbop
Last active June 8, 2026 00:03
Show Gist options
  • Select an option

  • Save rcbop/4acabfb15db6b361698ebd76ce5646e2 to your computer and use it in GitHub Desktop.

Select an option

Save rcbop/4acabfb15db6b361698ebd76ce5646e2 to your computer and use it in GitHub Desktop.
Jenkins pipeline script for Docker cleanup (remove dangling images and exited containers) in a given build agent
node("${params.BUILD_AGENT}") {
stage('Dangling Containers') {
sh 'docker ps -q -f status=exited | xargs --no-run-if-empty docker rm'
}
stage('Dangling Images') {
sh 'docker images -q -f dangling=true | xargs --no-run-if-empty docker rmi'
}
stage('Dangling Volumes') {
sh 'docker volume ls -qf dangling=true | xargs -r docker volume rm'
}
}
@ScottBrenner

Copy link
Copy Markdown

@guice

guice commented Jan 17, 2020

Copy link
Copy Markdown

Don't forget the --volumes flag, too: docker system prune -af --volumes (-af for good measure).

@rcbop

rcbop commented Jan 20, 2020

Copy link
Copy Markdown
Author

Sorry guys, this is a very old gist, I just copied a few snippets that I had without reading and published them here.. at the time I wrote this, system prune feature was not available and it's the best way now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment