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.
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.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you @Kawan Diogo !!! greatly appreciated
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This article on the developer community could hold some pointers, https://community.developer.atlassian.com/t/bulk-un-archive-tickets-using-rest-api-scriptrunner/50028/3
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Me neither, I'm also not a big fan on scripting.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Marc,
From what I read here, as of Jira 8.1,
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.