Based on the idea by @C4stor I have made this working and improved version (careful his version doesn't work...) I find it more readable from @daggett 's version
import hudson.model.Result import hudson.model.Run import jenkins.model.CauseOfInterruption.UserInterruption def abortPreviousBuilds() { Run previousBuild = currentBuild.rawBuild.getPreviousBuildInProgress() while (previousBuild != null) { if (previousBuild.isInProgress()) { def executor = previousBuild.getExecutor() if (executor != null) { echo ">> Aborting older build #${previousBuild.number}" executor.interrupt(Result.ABORTED, new UserInterruption( "Aborted by newer build #${currentBuild.number}" )) } } previousBuild = previousBuild.getPreviousBuildInProgress() } }