Deleting All Attachments through Groovy

William McGuinness November 14, 2012

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

2 votes
Answer accepted
JamieA
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.
November 14, 2012

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.

Jered Biard August 11, 2013

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!

James Batey November 21, 2013

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

0 votes
Jered Biard August 11, 2013

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!

fabio marcelo October 8, 2014

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