JIRA Script fix missing attachments in database

Viktor Galunov October 31, 2017

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

0 votes
Viktor Galunov November 1, 2017

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!!!

Viktor Galunov November 1, 2017

deleteAttachment does not do reindex.

So whole JIRA reindex should be done afterwards.

Suggest an answer

Log in or Sign up to answer