Deleting All Attachments through Groovy

Hi everyone,

I am using the wonderful Script Runner plugin and have a post-function set up to clone an issue into another project. I had wanted to NOT copy any of the attachments to the new issue, or alternately delete all the attachments from the new issue.

My first thought was adding something to the "Additional issue actions" section at the bottom for groovy script that will delete all attachments, but I'm not sure about what to write here or the syntax.

Does anyone know how to delete all attachments on an issue in Groovy?

Thanks in advance,
Will

2 answers

1 accepted

Accepted Answer
1 vote

Hello....

I'm afraid, looking at the code, that the attachments are copied after the additional code is run... this may have been a mistake on my part, can't remember. Not sure why I didn't put in provision for copying attachments to be optional.

You could either modify the source... just remove one line from com.onresolve.jira.groovy.canned.workflow.postfunctions.CopyIssueWithAttachments, no compilation necessary, or, add another script post-function pointing to a file containing this code:

import com.atlassian.jira.component.ComponentAccessor

def attachmentManager = ComponentAccessor.getAttachmentManager()

attachmentManager.getAttachments(issue).attachments.each {
    attachmentManager.deleteAttachment(it)
}

Completely untested, but should work.

Jamie, can you provide some guidance on where to find the version of com.onresolve.jira.groovy.canned.workflow.postfunctions.CopyIssueWithAttachments so that it can be modified in my existing plugin install? I attempted to use the above suggestion to delete the attachments on the Create transition of the cloned (new) issue, but it didn't do anything. I am using version 2.1.9 of Script Runner and 5.2.8 of Jira. Thanks!

Hi Jamie,

Trying to implement your workaround as this is something we need. We are running Script Runner 2.1.15 and JIRA 6.0.8. This comment is quite old but I don't see the option to remove attachments in the clone issues post function so I'm assuming this is still a potentially required workaround?

We get an error trying to run this script and i'm wondering if things have changes since this comment was posted?

javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: Exception evaluating property 'attachments' for java.util.ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: attachments for class: com.atlassian.jira.issue.attachment.Attachment

Jamie, can you provide some guidance on where to find the version of com.onresolve.jira.groovy.canned.workflow.postfunctions.CopyIssueWithAttachments so that it can be modified in my existing plugin install? I attempted to use the above suggestion to delete the attachments on the Create transition of the cloned (new) issue, but it didn't do anything. I am using version 2.1.9 of Script Runner and 5.2.8 of Jira. Thanks!

Hi Jered, I have the same problem that you.You got to make this work. Jamie, Please where i can put this groovy code ? Best Regards

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

21,505 views 2 7
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you