how do I automatically delete task attachments older than n days?

Kirill March 5, 2023

I have a centos7 server, the attachments directory has become large, how can I automatically delete attachments that are older than n days? Perhaps there is a template ?

1 answer

0 votes
Blue Ridge Consultants
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.
March 5, 2023

Hello @Kirill

If you are on Data Center, Automation for Jira has the ability to do this with a Scheduled job.  You can have the job run a JQL like

status = Done AND updated <= -90d

Then have the job delete all attachments.

 

If you are on server (just a reminder server goes out of support in 1 year) I'd recommend taking a look at Script Runner for Jira.  There is a built-in script that does exactly what you want.  You can also write a scripted job that does the above.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.AttachmentManager
AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager()
def attachments = issue.get("attachment");
attachments.each {
attachment -> attachmentManager.deleteAttachment(attachment)
}

 

If you have any further questions feel free to reach out.  Thanks!

Kirill March 5, 2023

I didn't understand how to make this script delete attachments older than n days?

Blue Ridge Consultants
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.
March 5, 2023

Something like this:

Issues.search('status = Done AND updated <= -90d').each { issue ->
// delete attachments
def attachments = issue.get("attachment");
attachments.each {
attachment -> attachmentManager.deleteAttachment(attachment)
}

https://library.adaptavist.com/entity/jql-search

 

You can also take a look at the

com.atlassian.jira.bc.issue.search.SearchService

 as well as another way to execute JQL.

https://docs.atlassian.com/software/jira/docs/api/8.1.0/com/atlassian/jira/bc/issue/search/SearchService.html

Like Kirill likes this
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 5, 2023

Well, this script will find the issues that were not updated for 90 days and delete all their attachments. It may not be exactly what you wanted, beware!

Kirill March 5, 2023

Сan you help?

Kirill March 5, 2023

We need to delete attachments from the server older than 2 years, but the tasks should remain.

Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 5, 2023

OK, let me be more precise. The script will not delete the issues.

But, if an issue has an attachment "A" 10 years old, and another "B" 2 days old, then the issue was updated 2 days ago and nothing will be deleted. If I understand you correctly, "A" should be deleted in this case.

Kirill March 5, 2023

yes, "A" should be deleted. Delete all attachments older than 2 years

Kirill March 6, 2023

is it possible to delete attachments with an API request ?

Morgan DUBUISSON
Contributor
March 6, 2023

Hi,

yes I think you can, but you have to script...

Find issue and parse result for find attachment and loop on it

https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/issue-getIssue

https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/attachment-removeAttachment

 

Best Regards

Suggest an answer

Log in or Sign up to answer