Skip to main content
Book your demo
Example scripts
To homepage
Jira
Cloud icon
Cloud

Archive Unused Spaces

Created 1 year ago, Updated 0 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira
Language |
groovy
// Number of days since last update on any work item
int numberOfDaysWithoutWorkItemUpdateLimit = 10

// A list to store the keys of spaces to be archived
def spaceKeysToArchive = []

Spaces.getAllSpaces().each { space ->

    // If the space contains no work items then add it to the list of spaces to be archived
    if (space.getInsight().totalIssueCount == 0) {
        spaceKeysToArchive.push(space.key)
    }

    // If the space has not updated any work items in the specified timeframe then add it to the list of spaces to be archived
    def lastWorkItemUpdateTime = space.getInsight().lastIssueUpdateTime
    if (lastWorkItemUpdateTime) {
        def lastWorkItemUpdateTimestamp = Date.from(lastWorkItemUpdateTime.toInstant())
        def currentDate = new Date()
        def millisecondsDifference = Math.abs(currentDate.time - lastWorkItemUpdateTimestamp.time)
        def daysSinceLastWorkItemUpdate = millisecondsDifference / (1000 * 60 * 60 * 24) as double
        def roundedDaysSinceLastWorkItemUpdate = Math.round(daysSinceLastWorkItemUpdate)

        if (roundedDaysSinceLastWorkItemUpdate >= numberOfDaysWithoutWorkItemUpdateLimit) {
            spaceKeysToArchive.push(space.key)
        }
    }
}

// Loop over each space key in the list of spaces to be archived
spaceKeysToArchive.each { key ->
    // Archive the space
    // Note you must be on a Premium or Enterprise plan to be able to use this API
    post("/rest/api/3/project/${key}/archive")
            .header("Content-Type", "application/json")
            .asObject(Map)

    logger.info("Archived the space with the key of ${key}")
}

"Archiving completed. Check the logs tabs to see what spaces were archived and for any errors."

Having an issue with this script?
Report it here