Jira Cleaner Script: An Automated Script to Clear your Jira Storage

1 minute read

Published:

Preface

If you are using premium Jira, you probably know that you do not have more than 2 GB of storage space. As a result, storage is gradually filled by adding more attachments to the issues. Therefore, we will have to clear the storage of old attachments. Because deleting old attachments individually is very time consuming and tedious, running a piece of code can save a lot of our time.

The following code snippet does this for us:

# -*- coding: utf-8 -*-

import requests
from requests.auth import HTTPBasicAuth

USER = 'YOU_USER_NAME'
API_KEY = 'YOUR_API_KEY'

ISSUE_GET_URL = 'CUSTOMIZED_ISSUE_URL' # format is like: 'https://YOUR_DOMAIN.atlassian.net/rest/api/3/issue/' 
ATTACHMENT_DELETE_URL = 'CUSTOMIZED_ATTACHMENT_URL' # format is like: 'https://YOUR_DOMAIN.atlassian.net/rest/api/3/attachment/'

PROJECT_KEY = 'YOUR_PROJECT_KEY'
START_ISSUE_ID = 1
END_ISSUE_ID = 1000

###########################################################################

def get_and_delete_issues():
    auth = HTTPBasicAuth(USER, API_KEY)
    for i in range(START_ISSUE_ID, END_ISSUE_ID):
        issue_key = PROJECT_KEY + '-{}'.format(str(i))
        issue_url = ISSUE_GET_URL + issue_key
        response = requests.get(issue_url, auth=auth)
        if response.status_code == 200:
            print('information of issue with key: {} obtained'.format(issue_key))
            data = response.json()
            issue_type = data['fields']['issuetype']['name']
            if issue_type == 'Bug': # delete bugs attachments only. you can remove this part to delete all types
                attachments = data['fields']['attachment']
                for attachment in attachments:
                    delete_issue_attachments(attachment['id'])
            else:
                print('issue with key: {} was not a bug'.format(issue_key))
        else:
            print('failed to obtain issue with key: {}, status code: {}'.format(issue_key, response.status_code))

###########################################################################

def delete_issue_attachments(attachment_id):
    auth = HTTPBasicAuth(USER, API_KEY)
    url = ATTACHMENT_DELETE_URL + attachment_id
    response = requests.delete(url, auth=auth)
    if response.status_code == 204:
        print('attachment with id: {} deleted'.format(attachment_id))
    else:
        print('failed to delete attachment with id: {}, status code: {}'.format(attachment_id, response.status_code))

###########################################################################

if __name__ == "__main__":
    get_and_delete_issues()

As you see, the code is quite straightforward and simple. just run pip install requests to install requests package and python jira_cleaner.py to execute the code. Don’t forget to replace the global variables with your own ones.

photo by Kowon vn

Leave a Comment