How to keep the 5 latest builds and prune all others

hi,

I’m setting up a Jenkinsfile pipeline which only keeps the last 5 build and deletes all others, im having some issues on figuring out how to do this with docker prune as i have seen lots of tags for time based pruning or user based pruning.

I have not been able to find anything on how to only keep X number of the latest builds, or if it is there i have missed it.

Any help on how to do this would be appreciated.

cheers in advanced.