Scriptrunner Script for Restoring and deleting archived Issues

Rebekka Heilmann _viadee_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 20, 2025

We are conducting a huge Cleanup and have a large amount of either archived projects or projects with partly archived issues.

I am looking for a Scriptrunner Script to delete all Issues (archived or not) from a project.

A first step could be a script to restore all archived issues from the project. I can then procede by using the "Delete Project" option, which deletes all issues as well.

2 answers

1 accepted

1 vote
Answer accepted
Kawan Diogo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2025

Hi @Rebekka Heilmann _viadee_ 

 

I have a solution to run in the Script Console of your Scriptrunner, as you discovered it would remove the project the following script only works to restore the archived issues

 


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.archiving.ArchivedIssueService
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.bc.issue.IssueService

// Set the project Key
def projectKey = "YOUR_PROJECT_KEY"

// Get your user to execute the action
def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Get necessary managers
def projectManager = ComponentAccessor.getProjectManager()
def issueManager = ComponentAccessor.getIssueManager()
def issueArchiveService = ComponentAccessor.getComponent(ArchivedIssueService)

// Look for the project
def project = projectManager.getProjectByCurrentKey(projectKey)
if (!project) {
return "Projeto com key '${projectKey}' não encontrado!"
}

// Get the issues ID of the project
def issueIds = issueManager.getIssueIdsForProject(project.id)

// Search for the archived ones
def archivedIssues = issueIds.collect { issueManager.getIssueObject(it) }
.findAll { issue -> issue && issue.isArchived() }

if (archivedIssues.isEmpty()) {
return "No archived issues found in the project: '${projectKey}'."
}

def sizearchive = archivedIssues.size()
def i = 0


while (i < sizearchive) {
def issue = archivedIssues[i] as String
log.warn(issue)
def validationResult = issueArchiveService.validateRestoreIssue(loggedInUser, issue, false)

issueArchiveService.restoreIssue(validationResult)
log.info("Issue ${issue} restored with sucess.")

i++
}

return "Restored ${archivedIssues.size()} issues archived from the project: '${projectKey}'."

 

Let me know if this help in your problem.

 

Best Regards.

Rebekka Heilmann _viadee_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 24, 2025

Works great! Thank you so much, @Kawan Diogo 

Now I only need to figure out a way to delete archived Issues, where the Project itself has been deleted. 

The script lists issues per Project - not sure how to access Issues without a project. Any ideas other than deleting them directly in the Database?

Kawan Diogo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 24, 2025

Great to hear it helps you @Rebekka Heilmann _viadee_ 

Talking about the archived issues without a project, i think you will need deleting them directly in the Database.

 

That's because these issues are now "orphaned" and are difficult to manage through the API or user interface.

With scriptrunner you can acess the database to perform some queries to validate the issues without a project.

Get all issues without a project:

SELECT id, issuenum, summary, archived
FROM jiraissue
WHERE project IS NULL
AND archived = TRUE;

 

If you don't have the archived column you can try this one:

SELECT ji.id, ji.issuenum, ji.summary, cs.cname AS status
FROM jiraissue ji
LEFT JOIN issuestatus cs ON ji.issuestatus = cs.id
WHERE ji.project IS NULL
AND cs.cname = 'Archived'; 

 

If you don't want delete the issue directly in the database, you can use a script using an array with the id's you can get in the selects above.

 

Something like this:

 

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()

// List of orphaned issue IDs (replace with IDs from the database query)
def orphanedIssueIds = [XXXXX, XXXXX] // Example IDs

orphanedIssueIds.each { issueId ->
def issue = issueManager.getIssueObject(issueId)
if (issue) {
issueManager.deleteIssueNoEvent(issue)
log.info("Deleted orphaned issue: ${issue.key}")
} else {
log.warn("Issue with ID ${issueId} not found or already deleted.")
}
}

return "Completed deletion of orphaned issues."

 

Hope that helps.

Rebekka Heilmann _viadee_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 24, 2025

Thank you @Kawan Diogo !!! greatly appreciated

Like Kawan Diogo likes this
0 votes
Marc - Devoteam
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 20, 2025
Rebekka Heilmann _viadee_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 23, 2025

Hi Marc,

i saw the article but was hoping someone would have already done the switch from archiving to unarchiving in the provided script in that thread :D 

I am not that big of a Scripter myself but will figure something out and post it here

Like Marc - Devoteam likes this
Marc - Devoteam
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2025

Hi @Rebekka Heilmann _viadee_ 

Me neither, I'm also not a big fan on scripting.

Jorge Guerreiro
Contributor
January 23, 2025

Hi Marc,

From what I read here, as of Jira 8.1,Screenshot 2025-01-23 at 16.16.42.png

I literally wrote a script to unarchive all of the issues from a project until I realised JQL does not help me on this any more. As far as I'm aware there is no way to get the archived issues with ScriptRunner.

Hope that helps

Suggest an answer

Log in or Sign up to answer