JIRA Script fix missing attachments in database


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.




1 answer

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()){

 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
Community showcase
Published Jan 08, 2019 in Jira

How to Jira for designers

I’m a designer on the Jira team. For a long time, I’ve fielded questions from other designers about how they should be using Jira Software with their design team. I’ve also heard feedback from other ...

1,187 views 5 10
Read article

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