title: Delete jenkins builds during Promote / promotion step
body: <p><strong>Jenkins</strong>: Version 1.525<br/>
  <strong>Jenkins Server URL</strong>: <a href="http://my.jenkins.server.com:9040" rel="nofollow">http://my.jenkins.server.com:9040</a><br/>
  <strong>Linux</strong> Red Hat 5.3</p>
  
  <p><strong>Artifactory</strong>: Free version<br/>
  <strong>Artifactory Server URL</strong>: <a href="http://my.artifactory.server:8081/Artifactory" rel="nofollow">http://my.artifactory.server:8081/Artifactory</a></p>
  
  <p>I'm successfully able to build in Jenkins and upload artifacts to my Artifactory server under a respective repository. <br/><br/></p>
  
  <p>When a build occurs, the artifacts (ProjectA-1.0.0.25.tar.gz) goes to Artifactory under libs-snapshot-local repository. Here 1.0.0 is denoting Major, minor and interim version of a given release for an application/jenkins job: "ProjectA" in this case. Lets say 25 is the build number<br/>
  <br/><br/>
  When ProjectA build gets stable in Development, we promote a given build of that application release to INT or any other higher environment (QA/PrePROD etc).</p>
  
  <p>During this promotion process, we just select which build to promote and using Jenkins Promoted Build Plugin, we are able to do it successfully.<br/><br/></p>
  
  <p><strong>Now, what we need is:</strong></p>
  
  <ol>
  <li>During the promote process, I want to call a Groovy script, which will delete all Jenkins builds from Jenkins and Artifactory (libs-snapshot-local) for ProjectA's release 1.0.0 after "ProjectA-1.0.0.25.tar.gz" is successfully promoted to INT. Promotion part is working fine right now; All I need is a Groovy script which will delete Jenkins builds (1.0.0.1 to 1.0.0.24 and >= 1.0.0.25) in Jenkins and its associated artifacts from Artifactory repository (libs-snapshot-local).
  <br/></li>
  </ol>
  
  <p>Idea in our company is, once a release version's build is promoted for an application, all other builds / artifacts we have (in Jenkins/Artifactory) - we want to delete forever using a Groovy script. Someone will ask, what if I want to promote a different build#; well in our case, we dont want that. Simple rule is, if someone promotes ProjectA-1.0.0.25.tar.gz, then, delete ProjectA's builds / artifacts in Jenkins and Artifactory where build/artifact is other than 1.0.0.25 and continue with new release 1.1.0<br/></p>
  
  <p><br/></p>
  
  <p>The script with the following capability would be great.<br/>
  1. Use property files (jenkins.properties / artifactory.properties) - which will contains some variables about hostname/username/password etc if any.<br/>
  2. Use REST API to perform the deletion for a given application/job and given release (for ex 1.0.0)<br/>
  3. Can be used for both Jenkins/Artifactory deletion - if at command prompt, I say use this (Jenkins) property file -or that (Artifatory) - as in both cases, application and its release value will be same.<br/>
  4. We know that for promoting a build to INT (using Jenkins promote plugin), we'll always delete from Jenkins server and from Artifactory server only at libs-snapshot-release.
   <br/>Now if someone does promotion to QA (at a later time), then artifactory repository will be (libs-stage-local)</p>
  
  <p>In other words, we should call the Groovy script, pass some variables/values (REST) and tell which application/job to delete and what build release version it's. Then, it'll delete all builds except the one which a user will pass (i.e. 1.0.0.25)</p>
  
  <p>I'm new to both Groovy / using REST API for doing this "deletion" piece of work for Jenkins/Artifactory. If someone already has any sample script that does this kind of activity and if you can share, I'll tweak it according to my settings and see if I can see the above mentioned behaviour during promotion step. I have some time crunch in getting a working version of this script, would appreciate some script code doing the same task (instead of great people telling me to go through big documentations/links, I know that'll make me a better coder in Groovy but it'll delay the whole purpose for this post).</p>
  
  <p><strong>Thanks a lot.</strong></p>