JIRA Script fix missing attachments in database

Hello,

I have created a copy of productive JIRA server and decided to remove all attachments in order to keep the space.
Pretending to be a smart admin I simply applied rm -rf command to the attachments folder.
After that I restored only attachments for 1 project I really need.

But it was not very good for JIRA Health.
Jira reported 105472 missing attachments.

Unfortunatelly I have not found any scripted solution in answers.
That is why I decided to ask for JIRA script which will fix it.

 

--

Viktor

1 answer

This widget could not be displayed.

I have reworked gathering script provided by Atlassian to make him remove database metadata for not existed attachments ONLY!
I ran it in Adaptavist Script Console and It gave obvious timeout ERROR but in fact took about half an hour on my environment.
Because script checks all attachments in all issues inside all projects.

Question is not actual anymore.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.util.AttachmentUtils;

def projects = ComponentAccessor.getProjectManager().getProjectObjects();
for (project in projects){
def issueManager = ComponentAccessor.getIssueManager();
def issues = issueManager.getIssueIdsForProject(project.getId());
for (issueID in issues){
def issue = issueManager.getIssueObject(issueID);
def attachmentDirectory = AttachmentUtils.getAttachmentDirectory(issue);
def attachmentDirectoryPath = attachmentDirectory.getAbsolutePath();
for (attachment in issue.getAttachments()) {
def legacy_fullAttachmentPath = attachmentDirectoryPath + "/" + attachment.getId() + "_" + attachment.getFilename();
def legacy_potentialFile = new File(legacy_fullAttachmentPath);
def fullAttachmentPath = attachmentDirectoryPath + "/" + attachment.getId();
def potentialFile = new File(fullAttachmentPath);
if (!potentialFile.exists() && !legacy_potentialFile.exists()){
ComponentAccessor.getAttachmentManager().deleteAttachment(attachment);
}
}
}
}

 Scriptrunner is the best JIRA plugin!!!

deleteAttachment does not do reindex.

So whole JIRA reindex should be done afterwards.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

121 views 2 0
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