import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.util.BuildUtilsInfo
import org.ofbiz.core.entity.DelegatorInterface
final int JIRA_8_9_0 = 809000
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueManager = ComponentAccessor.issueManager
def delegatorInterface = ComponentAccessor.getComponent(DelegatorInterface)
def buildUtilsInfo = ComponentAccessor.getComponent(BuildUtilsInfo)
def subTasks = issue.getSubTaskObjects()
subTasks.each { subTask ->
// add a condition here if you want to delete selective subtasks
issueManager.deleteIssue(user, subTask, EventDispatchOption.ISSUE_DELETED, false)
if (buildUtilsInfo.applicationBuildNumber >= JIRA_8_9_0) {
delegatorInterface.removeByAnd("IssueVersion", [issueId: subTask.id, deleted: 'Y'])
}
}