How remove builds (as lot operation) on jenkins.

Allan Santos
1 min readJan 4, 2020

Method 1. With SCRIPT in jenkins console

Access <JENKINS_URL>/jenkins/script and execute script bellow.

def reallyDelete = false;  // GHANGE TO TRUE, TO REALLY DELETE
def range = “1–20”; // RANGE
def jobName = “ProjectTestDelete”; //JOB_NAME
def rs = Fingerprint.RangeSet.fromString(range, false);
def job = Jenkins.instance.getItemByFullName(jobName);
def builds = Jenkins.instance.getItemByFullName(jobName).getBuilds(rs);println(“Job: ${job.fullName}”);
println(“Found ${builds.size()} builds”);
builds.each{ b ->
if (reallyDelete) {
b.delete();
} else {
println(“Found match ${b}”);
}
}

Method 2. With CURL command line.

Get the API Token (All users have their own API token. To see a person’s API token)

1. Go to "People"
2. Select a user
3. Click "Configure"
4. Click "Show API Token"

After you have the API Token you can do a URL POST to clean your buids.

curl -X POST http://<USER>:<API_TOKEN>@<URL_JENKINS>/jenkins/job/<JOB_ID>/<BUILD_NUMBER>/doDelete

If you use, with a “for” operation on shell script you can delete many builds at once.

Sample

for buildNumber in {1..20}; do curl -X POST  http://jenkins:e7669c76796f8d92d98ef69595ffa9ac@localhost:8080/jenkins/job/ProjectTestDelete/$buildNumber/doDelete; done

Bonus: If you want to delete many things, you can do it, using regex.

Access <JENKINS_URL>/jenkins/script and execute script bellow.

import jenkins.model.*def matched = Jenkins.instance.items.findAll { job -> 
item.name =~ /<REGEX>/
}
matched.each { item ->
println item.name
item.delete()
}

--

--