Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,557,818
Community Members
 
Community Events
184
Community Groups

ScriptRunner script to delete attachments smaller than 10kb

I try to delete attachments like small jpgs/pngs that are coming with issues created by mail. I tried to do it by automation for Jira, but this app only allows for regular expression for file names I think.

So I'd like to delete them "trash-attachments" with a scriptrunner script, but it's my first script and I have real trouble getting on the right path. 

 

Does someone know how to do it or has at least some pathway to set me on.

3 answers

1 accepted

2 votes
Answer accepted
Patrick S
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 14, 2022

I just accomplished this by using a ScriptListener, and the script below.  You could use IssueUpdated, WorkLogged, etc as the Listener trigger.

You can tweak the script below for whatever your minimum desired file size is, or other attachments that you want to remove regularly.

https://yourJIRAurl/plugins/servlet/scriptrunner/admin/listeners

/*
Remove unhelpful attachments that clutter an issue, especially when they have a LOT of these attachments.
Primary target is smime.p7s files, but all files targeted here are ultimately small files that get attached by an Outlook reply. They're metadata for the email, or logo images from signature blocks.
*/

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()

// Use the next 2 lines if you want to test this in the script console with a specified issue. Otherwise, "issue" will be the current issue.
// String issuekey = "CUSTSVC-12569" // Change this issuekey to the ticket you will be testing with.
// MutableIssue issue = issueManager.getIssueObject(issuekey)

def attachments = ComponentAccessor.attachmentManager.getAttachments(issue)
int deletedAttachmentsCount = 0;

for (attachment in attachments) {

def attachmentname = attachment.getFilename()
def attachmentsize = attachment.getFilesize()
if (attachmentsize <= 1024 || attachmentname.endsWith(".p7s")) {
deletedAttachmentsCount = deletedAttachmentsCount + 1
log.info attachmentname + " = " + attachmentsize + " bytes is being removed from " + issuekey + ". See <task where you logged your work for deploying the script> for details."
ComponentAccessor.attachmentManager.deleteAttachment(attachment)
}

}

if (deletedAttachmentsCount > 0){log.info "Purged " + deletedAttachmentsCount.toString() + " small attachments that were likely added by Outlook from " + issuekey + "."}

  

Thanks very much! what a great sript.

Like Patrick S likes this
Patrick S
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jul 18, 2022

Quite welcome!

0 votes
Andre Serrano
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.
Sep 10, 2020

Maybe using ScriptRunner Mail Handler could be a good way out as well?

0 votes
Ravi Sagar _Sparxsys_
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.
Sep 09, 2020

Hi @Stephan Maass 

Can you share your script?

Some code snippets for you to get started.

def attachments = ComponentAccessor.attachmentManager.getAttachments(issue)
//Get attachment size
attachments*.filesize

//Delete the first attachment
ComponentAccessor.attachmentManager.deleteAttachment(attachments.first())

You can write condition to only delete the attachment based on the file size.

I hope it helps.

Ravi

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events