How can I delete attachments from Issue on workflow post-function?

Aliaksei Mishenin November 11, 2013

Question in a subject. We're handle sensitive data in attachments and want to purge all of them after reporter will aprove that he received them. Is there an easy way to automate this process?

6 answers

1 accepted

3 votes
Answer accepted
Bhushan Nagaraj
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 11, 2013

Download post-function plugin from here.

https://bitbucket.org/bhushan154/jira-delete-attachments-post-function/downloads#download-263211

Haven't tested it yet. Let me know if you have any issues.

sanjay December 28, 2020

Hi Bhushan Sir,

I have installed this jar on local host but not getting how it will work can you please guide me !

Regards

Sanjay Dhandare

sanjay December 28, 2020

Hi Sir I got it it will appear as a custom functionality in workflow ! 

Great work ! 

4 votes
Henning Tietgens
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 11, 2013

You can use the Script Runner plugin and code like this in a scripted workflow postfunction.

import com.atlassian.jira.component.ComponentAccessor

def attachmentManager = ComponentAccessor.attachmentManager def attachments = attachmentManager.getAttachments(issue) attachments.each {attachment -> attachmentManager.deleteAttachment(attachment) }

 

 

Stefano Rosario Aruta January 23, 2019

i have a error on -&gt can you help me?

Henning Tietgens
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.
January 23, 2019

It has to be „->“ instead of „->“

Like Stefano Rosario Aruta likes this
Stefano Rosario Aruta January 23, 2019

what is a import i need to add on this post function?

Henning Tietgens
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.
January 23, 2019

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.AttachmentManager

should do the trick. :-)

Like Stefano Rosario Aruta likes this
Henning Tietgens
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.
January 24, 2019

I updated/simplified the script to incorporate your comments. This was not possible on mobile yesterday.

Like Stefano Rosario Aruta likes this
Stefano Rosario Aruta January 24, 2019

sure dude, thank you very much
can you help my  here below  i dont want copy attachment but i want create a link of the origina attachment, so i want that sub-task have a attahcment of  issue parent  and dont copy attachment. 

can you help me with this?



import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.config.SubTaskManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.Attachment;
import com.atlassian.jira.issue.IssueFactory;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;

if (issue.getIssueType().getName() != "Sub-task") {
if (ComponentAccessor.getAttachmentManager() .getAttachments(issue).size() >= 1) {
for(int i = 0; i < ComponentAccessor.attachmentManager.getAttachments(issue).size(); i++) {
//__________________________________________________________________________________________________________________________________________________
Issue parentIssue = issue
def att = parentIssue.getAttachments()[i];
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
IssueFactory issueFactory = ComponentAccessor.getIssueFactory();
String subTaskId = ComponentAccessor.getConstantsManager().getAllIssueTypeObjects().find { it.getName() == "Sub-task" }.id

IssueManager issueManager = ComponentAccessor.getIssueManager();
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager();
AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager()
//__Fase
MutableIssue newSubTask = issueFactory.getIssue();
newSubTask.setSummary("Sub-Task Attachment: " + (i+1));
newSubTask.setDescription("Sub-Task with an Attachment");
newSubTask.setParentObject(parentIssue);
newSubTask.setReporterId(currentUser.username);
newSubTask.setProjectObject(parentIssue.getProjectObject());
newSubTask.setIssueTypeId(subTaskId);
//__Fase
issueManager.createIssueObject(currentUser, newSubTask);
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, currentUser);

attachmentManager.copyAttachment(att, currentUser, newSubTask.getKey().toString())
//__________________________________________________________________________________________________________________________________________________
}
}
}
Henning Tietgens
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.
January 24, 2019

So you want the Sub-task to have an attachment (displayed as a normal attachment) which is only a link to the attachment of the parent issue? I don't think this is possible.

You maybe able to create links in a field (like the description) to the parent attachments, but not have these displayed as attachments.

def pathManager = ComponentAccessor.getAttachmentPathManager()
def filePath = PathUtils.joinPaths(pathManager.attachmentPath, issue.projectObject.key, issue.key, att.id.toString())

Or you have to copy the attachments from the parent issue to the Sub-task.

Henning 

Stefano Rosario Aruta January 24, 2019
Henning Tietgens
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.
January 24, 2019

Try this to copy each attachment to a single sub-task.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import com.atlassian.jira.util.PathUtils

final SUBTASK_ISSUETYPE_NAME = "Sub-task"

if (issue.issueType.name != SUBTASK_ISSUETYPE_NAME) {
def issueFactory = ComponentAccessor.issueFactory
def constantsManager = ComponentAccessor.constantsManager
def issueManager = ComponentAccessor.issueManager
def subTaskManager = ComponentAccessor.subTaskManager
def pathManager = ComponentAccessor.attachmentPathManager
def attachmentManager = ComponentAccessor.attachmentManager

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def subTaskId = constantsManager.allIssueTypeObjects.find{it.name == SUBTASK_ISSUETYPE_NAME}.id

issue.attachments.eachWithIndex{att, i ->

def newSubTask = issueFactory.getIssue()
newSubTask.setSummary("Sub-Task Attachment: " + (i + 1))
newSubTask.setDescription("Sub-Task with an Attachment")
newSubTask.setParentObject(issue)
newSubTask.setReporterId(currentUser.username)
newSubTask.setProjectObject(issue.projectObject)
newSubTask.setIssueTypeId(subTaskId)

issueManager.createIssueObject(currentUser, newSubTask)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, currentUser)

def filePath = PathUtils.joinPaths(pathManager.attachmentPath, issue.projectObject.key, issue.key, att.id.toString())
def atFile = new File(filePath)
if (atFile.exists()) {
try {
if (atFile.canRead()) {
attachmentManager.createAttachment(
(new CreateAttachmentParamsBean.Builder(
atFile,
att.filename,
att.mimetype,
att.authorObject,
newSubTask)
)
.createdTime(att.created)
.copySourceFile(true)
.build()
)
}
}
catch (SecurityException se) {
log.warn("Could not read attachment file. Not copying. (${se.message})")
}
} else {
log.warn("Attachment file does not exist where it should. Not copying.")
}
}
}
Stefano Rosario Aruta January 24, 2019

it' doesn't work there isn't attahcment in sub-task

Henning Tietgens
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.
January 24, 2019

Ok, simplify it:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Logger

final SUBTASK_ISSUETYPE_NAME = "Sub-task"

if (issue.issueType.name != SUBTASK_ISSUETYPE_NAME) {
def issueFactory = ComponentAccessor.issueFactory
def constantsManager = ComponentAccessor.constantsManager
def issueManager = ComponentAccessor.issueManager
def subTaskManager = ComponentAccessor.subTaskManager
def attachmentManager = ComponentAccessor.attachmentManager

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def subTaskId = constantsManager.allIssueTypeObjects.find{it.name == SUBTASK_ISSUETYPE_NAME}.id

issue.attachments.eachWithIndex{att, i ->

def newSubTask = issueFactory.getIssue()
newSubTask.setSummary("Sub-Task Attachment: " + (i + 1))
newSubTask.setDescription("Sub-Task with an Attachment")
newSubTask.setParentObject(issue)
newSubTask.setReporterId(currentUser.username)
newSubTask.setProjectObject(issue.projectObject)
newSubTask.setIssueTypeId(subTaskId)

issueManager.createIssueObject(currentUser, newSubTask)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, currentUser)


attachmentManager.copyAttachment(att, att.authorObject, newSubTask.key)
}
}

The previous script is for Jira < 7.0.

Henning 

Like Stefano Rosario Aruta likes this
Stefano Rosario Aruta January 24, 2019

have a last version jira, but this script make a a new file on sub-task?

i think that the solution is to after this script delete a attachment on issue and add a post function at last transaction in the sub-task where copy attachment on issue and after delete on sub-task

Henning Tietgens
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.
January 24, 2019

Yes, this script creates one sub-task for each attachment. Isn't this what you want? If you want to delete the attachments from the parent issue after creating the sub-task simply add

issue.attachments.each{att ->
attachmentManager.deleteAttachment(att)
}

before the last }

Henning

Stefano Rosario Aruta January 24, 2019

yes, i idid but i don't know how to add  sub task attchament at main issue in the attchament field

Henning Tietgens
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.
January 24, 2019

For the last transaction of the sub-task you could use

import com.atlassian.jira.component.ComponentAccessor

final SUBTASK_ISSUETYPE_NAME = "Sub-task"

if (issue.issueType.name == SUBTASK_ISSUETYPE_NAME) {
def attachmentManager = ComponentAccessor.attachmentManager
issue.attachments.each{att ->
attachmentManager.copyAttachment(att, att.authorObject, issue.parentObject.key)
attachmentManager.deleteAttachment(att)
}
}

to transfer the attachments of the sub-task back to the parent issue.

Does this help?

Henning

Like Stefano Rosario Aruta likes this
Stefano Rosario Aruta January 24, 2019

amazing dude, you are masterpiece

Henning Tietgens
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.
January 24, 2019

You're welcome :-)

Stefano Rosario Aruta January 24, 2019

i think that the solution is to after this script delete a attachment on issue and add a post function at last transaction in the sub-task where copy attachment on issue and after delete on sub-task

Code to create Sub-task with a single attachment:

//Code make by Henning Tietgens
import
com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Logger

final SUBTASK_ISSUETYPE_NAME = "Sub-task"

if (issue.issueType.name != SUBTASK_ISSUETYPE_NAME) {
def issueFactory = ComponentAccessor.issueFactory
def constantsManager = ComponentAccessor.constantsManager
def issueManager = ComponentAccessor.issueManager
def subTaskManager = ComponentAccessor.subTaskManager
def attachmentManager = ComponentAccessor.attachmentManager

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def subTaskId = constantsManager.allIssueTypeObjects.find{it.name == SUBTASK_ISSUETYPE_NAME}.id

issue.attachments.eachWithIndex{att, i ->

def newSubTask = issueFactory.getIssue()
newSubTask.setSummary("Sub-Task Attachment: " + (i + 1))
newSubTask.setDescription("Sub-Task with an Attachment")
newSubTask.setParentObject(issue)
newSubTask.setReporterId(currentUser.username)
newSubTask.setProjectObject(issue.projectObject)
newSubTask.setIssueTypeId(subTaskId)

issueManager.createIssueObject(currentUser, newSubTask)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, currentUser)


attachmentManager.copyAttachment(att, att.authorObject, newSubTask.key)
}
}

After Second POST-Fucntion to delete every Attachment in the main issue:

//
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.AttachmentManager

AttachmentManager attachmentManager = ComponentAccessor.attachmentManager

def attachments = attachmentManager.getAttachments(issue)
attachments.each {attachment ->;
attachmentManager.deleteAttachment(attachment)
}

And this code is for sub-task Transaction(POST_Function): this script copy Attahcment on the main after delete every attachment in this sub-task

//Code make by Henning Tietgens

import
com.atlassian.jira.component.ComponentAccessor

final SUBTASK_ISSUETYPE_NAME = "Sub-task"

if (issue.issueType.name == SUBTASK_ISSUETYPE_NAME) {
def attachmentManager = ComponentAccessor.attachmentManager
issue.attachments.each{att ->
attachmentManager.copyAttachment(att, att.authorObject, issue.parentObject.key)
attachmentManager.deleteAttachment(att)
}
}

 ok

1 vote
Andrey Markelov
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 11, 2013

I think I can expand my add-on https://marketplace.atlassian.com/plugins/ru.mail.jira.plugins.attachdeleter to add new post-function. I will create a issue in my bug tracker

Aliaksei Mishenin November 11, 2013

That will be a very nice feature to have!

Andrey Markelov
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 12, 2013

Done it. https://marketplace.atlassian.com/plugins/ru.mail.jira.plugins.attachdeleter

Next version will provide options by date

0 votes
Rajesh Ramankutty January 9, 2020

Hi i am using smart attachment plugin

I need to delete the attachements in some categories after the invoice is been done in the title so kindly help me to write a script in the post fuction.'

For ex : i need only input file and output file from the attachment and i need to delete other files from the attachment after the transition is been done to invoice.

0 votes
RambanamP
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 11, 2013

if you need to automate then you have to write postfunction or listener to do that job

else you can delete attachments by using rest api

/rest/api/2/attachment/{id}(DELETE)

check this

https://docs.atlassian.com/jira/REST/latest/#d2e3993

free plugin is there to delete all attachements at once

https://marketplace.atlassian.com/plugins/ru.mail.jira.plugins.attachdeleter

Aliaksei Mishenin November 11, 2013

That's an interesting plugin but still not a lot difference between manual deleting.

Maybe there is a way to check existence of attachments in Issue on transition?

0 votes
rambabu patina
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 11, 2013

Try using ComponentAccessor.getAttachmentManager()

And see similar artical.

Hope this helps you

Patina

Aliaksei Mishenin November 11, 2013

Thanks, but writing a plugin for this procedure isn't an easy way for me :)

Suggest an answer

Log in or Sign up to answer