+* This example script console script shows how to fetch all projects and archive any projects which have 0 issues or projects where the issues have not been updated in X days.
+* Note: you must be on a Premium or Enterpise plan to be able to use this archiving API
+* "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
+* license shall be granted a non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed or
+* amended and must be included in any circumstances where the code snippet is shared by You or a third party."
+// Enter the number of days below
+def projectKeysToArhcive = [];
+// Define the number of items to be returned in each page of the API call
+// Define where to start at in the API call
+// Define an Array for storing all items returned
+// Loop over the Pages to be returned while their are still more pages of projects to fetch
+ // Get all the projects
+ def getProjects = get('/rest/api/3/project/search')
+ .header('Content-Type', 'application/json')
+ .queryString('expand', 'insight')
+ .queryString('startAt', startAt)
+ .queryString('maxResults', pageSize)
+ assert getProjects.status >= 200 && getProjects.status < 300
+ // Get the items returned
+ def itemsOnPage = getProjects.body.values
+ // If there are no more projects to fetch exit out of the loop
+ if (itemsOnPage.isEmpty()) {
+ // Add the items to the list
+ allItems.addAll(itemsOnPage)
+ // Increment the starting index for the next page
+// Loop over each project
+allItems.each { project ->
+ // If the project contains 0 issues add its key to the Array of projects to be archived
+ if (project.insight.totalIssueCount == 0) {
+ projectKeysToArhcive.push(project.key)
+ // If the project has not had any issues in it updated in the specified timeframe then add its key to the Array of projects to be archived
+ if (project.insight.lastIssueUpdateTime != null) {
+ def timestamp = Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ", project.insight.lastIssueUpdateTime)
+ def currentDate = new Date()
+ def millisecondsDifference = Math.abs(currentDate.time - timestamp.time)
+ def daysBetween = millisecondsDifference / (1000 * 60 * 60 * 24)
+ def roundedDaysBetween = Math.round(daysBetween)
+ if (roundedDaysBetween >= numberOfDays)
+ projectKeysToArhcive.push(project.key)
+// Loop over each key in the array of projects to be archived
+projectKeysToArhcive.each { key ->
+ // Note you must be on a Premium or Enterpise plan to be able to use this API
+ def archiveIssues = post("/rest/api/3/project/${key}/archive")
+ .header("Content-Type", "application/json")
+ logger.info("archived the project with the key of ${key}")
+return "Archiving completed. Check the logs tabs to see what projects were archived and for any errors."