Delete all subtasks of a parent issue as a workflow post function

Solution

  • Use a ScriptRunner workflow post function to automatically delete all subtasks of a parent issue

Step-by-step guide

  1. Choose JIRA Admin and find or type "." and then "Workflows"
  2. Choose the Edit link under the Operations column for the workflow
  3. Choose the transition you want to alter
  4. Choose the Post Functions tab 
  5. Choose Add post function link
  6. Click on the Script Post-Function radio button and then Add
  7. Click on the Custom script post-function link
  8. Add the below code to the "Inline script" blue text box

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
// for JIRA v6.*
def user = ComponentAccessor.getJiraAuthenticationContext().user.directoryUser
// for JIRA v7.*
// def user = ComponentAccessor.getJiraAuthenticationContext().user
def issueManager = ComponentAccessor.getIssueManager()
// will return a Collection<Issue> with all the subtasks of the current issue
def subTasksList = issue.getSubTaskObjects()
for (subTask in subTasksList) {
    // add a condition here if you DO NOT want to delete all the sub-tasks
    issueManager.deleteIssue(user, subTask, EventDispatchOption.ISSUE_DELETED, false)
}