Discard old builds in Jenkins

:heavy_exclamation_mark: This post is older than a year. Consider some information might not be accurate anymore. :heavy_exclamation_mark:

How to remove old builds as scripted pipeline in a Jenkinsfile? Not only I fail to see the sexiness in this new approach, but also I dislike the groovy syntax. In the end add I came up with several solutions to accomplish my goal: Keep only the last 10 builds. There are several ways and solutions. It was a huge try and error approach.

Set properties

Solution 1: Add before the node instruction the properties. This post help me a lot.

properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', artifactDaysToKeepStr: '14', artifactNumToKeepStr: '10', daysToKeepStr: '14', numToKeepStr: '10']]])

def isMaster = (env.BRANCH_NAME == 'master')
def version = null

node {
  // ..

Define Project Properties

Solution 2: You can also use the def declaration.

/* Only keep the 10 most recent builds. */
def projectProperties = [
    [$class: 'BuildDiscarderProperty',strategy: [$class: 'LogRotator', numToKeepStr: '10']],
Please remember the terms for blog comments.