I’m working with the Jira Python library and I can successfully add labels to tickets like this:
ticket.fields.labels.append("NEWTAGNAME")
However, I’m struggling to find a way to delete existing labels. I’ve spent hours searching through documentation and online resources but can’t figure out the correct syntax. I attempted to use a similar approach that works for removing components, but it fails for labels:
# This approach fails:
ticket.update(update={"labels": [{"remove": {"name": "OLDTAGNAME"}}],},)
# This works fine for components:
ticket.update(update={"components": [{"remove": {"name": "OLDCOMPONENTNAME"}}],},)
I need to programmatically remove labels from hundreds of tickets, so doing it manually isn’t really an option. The web interface bulk editor isn’t working properly for me either.
Here’s a simplified version of what I’m trying to accomplish:
#!/usr/bin/env python3
import os
from jira import JIRA
API_TOKEN_VAR = 'JIRA_TOKEN'
SERVER_URL = 'https://company.atlassian.net'
SEARCH_QUERY = 'labels in (DEPRECATEDTAG) and labels not in (NEWTAG)'
def process_tickets():
token = os.environ[API_TOKEN_VAR]
jira_client = JIRA(SERVER_URL, token_auth=token)
tickets = jira_client.search_issues(SEARCH_QUERY)
counter = 1
for ticket in tickets:
print(f'Processing {counter} - {ticket.key}')
print(f'Current labels: {ticket.fields.labels}')
# This works - adding new label
ticket.fields.labels.append('NEWTAG')
# This doesn't work - removing old label
# ticket.update(update={"labels": [{"remove": {"name": "DEPRECATEDTAG"}}],},)
# Apply changes
ticket.update(fields={"labels": ticket.fields.labels})
counter += 1
if __name__ == '__main__':
process_tickets()
What’s the correct way to remove labels from Jira issues using the Python API?