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

0 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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Apr 17, 2018 in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

809 views 2 19
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