Skip to main content
Example scripts
arrow icon
To homepage
Jira
Cloud icon
Cloud

Archive Unused Projects

Created 11 month(s) ago, Updated 1 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 issue
int numberOfDaysWithoutIssueUpdateLimit = 10

// A list to store the keys of projects to be archived
def projectKeysToArchive = []

Projects.getAllProjects().each { project ->

    // If the project contains no issues then add it to the list of projects to be archived
    if (project.getInsight().totalIssueCount == 0) {
        projectKeysToArchive.push(project.key)
    }

    // If the project has not updated any issues in the specified timeframe then add it to the list of projects to be archived
    def lastIssueUpdateTime = project.getInsight().lastIssueUpdateTime
    if (lastIssueUpdateTime) {
        def lastIssueUpdateTimestamp = Date.from(lastIssueUpdateTime.toInstant())
        def currentDate = new Date()
        def millisecondsDifference = Math.abs(currentDate.time - lastIssueUpdateTimestamp.time)
        def daysSinceLastIssueUpdate = millisecondsDifference / (1000 * 60 * 60 * 24) as double
        def roundedDaysSinceLastIssueUpdate = Math.round(daysSinceLastIssueUpdate)

        if (roundedDaysSinceLastIssueUpdate >= numberOfDaysWithoutIssueUpdateLimit) {
            projectKeysToArchive.push(project.key)
        }
    }
}

// Loop over each project key in the list of projects to be archived
projectKeysToArchive.each { key ->
    // Archive the project
    // 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 project with the key of ${key}")
}

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

Having an issue with this script?
Report it here