I want to delete attachments only based on attachment date but the script mentioned is fetching the issues based on JQL and deleting all the attachments in the Issue.
Do we have any way to delete attachment based on attachment date
Hi, @Asiya Fathima
You can use this script, it makes what you want. Needed time limits can be set in fromDate / toDate strings
/*
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/
package ConsoleExamples
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser) as JqlQueryParser
SearchService searchService = ComponentAccessor.getComponent(SearchService)
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String jqlQuery = "project = TEST AND attachments is not EMPTY"
Date fromDate = new Date("01/01/2023")
Date toDate = new Date("01/31/2023")
Query query = jqlQueryParser.parseQuery(jqlQuery)
SearchResults search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
List<Issue> results = search.results
results.each { Issue issue ->
List<Attachment> attachments = ComponentAccessor.attachmentManager.getAttachments(issue)
attachments.each { Attachment attachment ->
if (attachment.created >= fromDate.toTimestamp() && attachment.created <= toDate.toTimestamp()) {
ComponentAccessor.attachmentManager.deleteAttachment(attachment)
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, @Asiya fathima
Check please time limits in strings and set correct
Date fromDate = new Date("01/01/2023")
Date toDate = new Date("01/31/2023")
This condition checks, that attachment creation date is between this dates:
if (attachment.created >= fromDate.toTimestamp() && attachment.created <= toDate.toTimestamp())
If yes - then attachment is deleted.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Evgenii I tried to use this Script but the attachments are not getting deleted.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Asiya fathima your date format is wrong. Must be month/date/year.
Date fromDate = new Date("10/28/2012")
Date toDate = new Date("10/30/2012")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Evgenii , we tried with this format as well
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.