Skip to main content
Version: Release 23.1

Clean up Stale Releases

You can automate the cleanup of old releases by setting up a template with a cleanup script that will be periodically triggered by the criteria that you provide. You can use this procedure in cases where many releases are in a failed state that are older than the specified period of time.

Here is an example configuration that will create a release every 10 minutes that will try to abort all releases with the provided tags that are older than the provided age. You can copy and extend the provided script to suit your situation:

  1. Create a folder named System.
  2. In the System folder, create a template called Cleanup stale releases.
  3. Select the Cleanup stale releases template and go to the Variables section using the left navigation bar. You can now add the following variables:
  • pageSize of type Number (integer) with a default value of 10
  • tags of type Set
  • age of type Number (integer) with a default value of 1
  1. Go to the Release flow section and add a task named Abort stale releases of type Script > Groovy Script/Releasefile.
  2. In the Script section, paste the following as the groovy script task content:
        import com.xebialabs.xlrelease.api.v1.forms.ReleasesFilters
import com.xebialabs.xlrelease.api.v1.forms.ReleaseOrderMode
import com.xebialabs.xlrelease.api.v1.forms.TimeFrame
import java.util.Date
import java.time.LocalDate

def age = Integer.valueOf(releaseVariables["age"])

def filter = new ReleasesFilters()
filter.active = true
filter.tags = releaseVariables["tags"].asList()

filter.orderBy = ReleaseOrderMode.start_date

if (age > 0) {
filter.timeFrame = TimeFrame.RANGE
filter.from = new Date(LocalDate.now().minusYears(10).toEpochDay())
filter.to = new Date(LocalDate.now().minusMonths(age).toEpochDay())
}

def releases = []
def page = 0L
def pageSize = Long.valueOf(releaseVariables["pageSize"])
def hasMore = true

def cleanCount = 0

def totalReleases = releaseApi.searchReleases(filter).size()

while (hasMore) {
releases = releaseApi.searchReleases(filter, page, pageSize)
hasMore = !releases.isEmpty()
releases.each {
releaseApi.abort(it.id, "Aborted by cleanup script")
cleanCount++
println("Aborted release ${it.id}")
}
}

println ("Cleaned up ${cleanCount} out of total ${totalReleases}")
  1. Go to the Properties section and enter the values for the Run automated tasks as user and Password fields.
  2. Go to the Triggers section and disable the Allow concurrent triggered releases toggle.
  3. Create a new trigger of type Time: Schedule.
    • Provide a trigger title in the Title field. For example, Stale releases cleanup
    • Provide a release title in the Release title field. For example, Release cleanup
    • Select Schedule type to REPEAT
    • Set Schedule to 600 seconds
    • In the Tags field, add the tag named cleanup
    • Provide meaningful values for template variables: Page size, Release tags, and Release age.
  4. You can now manage this trigger from the Triggers management page.