## Introduction
Managing a cluttered Jira backlog with stale tickets—issues that have seen no activity for a long period—can be a persistent challenge for operations teams in startups and scaleups. These tickets can skew reporting metrics, hinder sprint planning, and waste developer time when revisited without context. Automating the identification and closing of stale Jira tickets helps maintain a clean, actionable backlog, enabling teams to focus on relevant work.
In this tutorial, we’ll build a workflow in n8n, a flexible open-source workflow automation tool, to automatically identify and close stale Jira tickets. This guide is intended for operations specialists, automation engineers, and startup CTOs looking to streamline backlog hygiene without manual intervention.
—
## What Problem Does This Automation Solve?
Stale Jira tickets:
– Accumulate over time due to inactivity
– Distort project health and velocity
– Increase maintenance overhead
By automatically detecting tickets untouched for a defined period and closing them with proper resolution and comments, the team gains:
– Improved backlog clarity
– Reduced manual ticket management effort
– More accurate reporting
– Scalable ticket hygiene processes
—
## Tools and Services Integrated
– **Jira Cloud API**: To query and update Jira tickets
– **n8n**: To orchestrate the automation workflow, including scheduling, filtering, and Jira API interactions
No additional third-party services are required, which keeps costs minimal and setup straightforward.
—
## Overview of the Workflow
1. **Trigger**: Scheduled trigger to run periodically (e.g., weekly)
2. **Query Jira**: Use Jira’s REST API to fetch tickets not updated within a predefined stale period (e.g., 30 days), filtered by project and status
3. **Filter Tickets**: Exclude tickets already closed or in specific statuses
4. **Update Tickets**: For each stale ticket, add a comment explaining auto-closure, then transition the ticket to a “Closed” status
5. **Logging**: Log actions or send a summary to Slack (optional)
—
## Step-by-Step Tutorial
### Prerequisites
– Access to n8n instance (self-hosted or cloud)
– Jira Cloud account with API access and permissions to modify tickets
– Jira API token for authentication
### Step 1: Create a New Workflow in n8n
– Log in to your n8n editor UI
– Create a new workflow and name it “Auto-Close Stale Jira Tickets”
### Step 2: Add a Cron Node to Schedule Automation
– Add the **Cron** node as the workflow’s trigger
– Configure it to run at your desired interval (e.g., every Sunday at midnight)
  – Repeat: Weekly
  – Day of the week: Sunday
  – Time: 00:00
### Step 3: Configure the HTTP Request Node to Query Jira
– Add an **HTTP Request** node named “Fetch Stale Tickets”
– Set the HTTP Method to `GET`
– For the URL, use Jira’s search API endpoint, something like:
  “`
  https://your-domain.atlassian.net/rest/api/3/search
  “`
– Under Query Parameters, add:
  – `jql`: `project = YOURPROJECT AND updated <= -30d AND statusCategory != Done`
    - This JQL retrieves tickets in project "YOURPROJECT" not updated in last 30 days and not already done/closed
  - `fields`: `key,status` (to minimize payload)
- Configure authentication:
  - Authentication Type: Basic Auth
  - Username: your Jira email
  - Password: Jira API token
  
- Set Response Format to JSON
### Step 4: Extract and Filter Retrieved Tickets
- Add a **Set** node if you want to trim data or rename fields for clarity
- Add a **IF** node to filter out tickets that might still require attention or are in statuses you want to exclude beyond the JQL filter
---
### Step 5: Loop Through Each Stale Ticket
- Add a **SplitInBatches** node following the IF node to process tickets one at a time, preventing API rate limits
### Step 6: Add a Comment to Each Ticket
- Add another **HTTP Request** node named "Add Comment"
- Configure for POST to:
  ```
  https://your-domain.atlassian.net/rest/api/3/issue/{{ $json["key"] }}/comment
  ```
- Payload Body (JSON):
  ```json
  {
    "body": "This ticket has been automatically closed due to inactivity of over 30 days. If this is in error, please reopen or create a new task."
  }
  ```
- Authentication: same as before
### Step 7: Transition the Ticket to Closed Status
- Add another **HTTP Request** node named "Transition Issue"
- POST to:
  ```
  https://your-domain.atlassian.net/rest/api/3/issue/{{ $json["key"] }}/transitions
  ```
- First, you need to know the ID of the "Close" transition for your Jira workflow.
- To get transition IDs, you can use GET:
  ```
  https://your-domain.atlassian.net/rest/api/3/issue/{{ $json["key"] }}/transitions
  ```
- Then, the payload for the POST:
  ```json
  {
    "transition": {
      "id": "
    }
  }
  “`
Make sure your user has permission to transition issues.
### Step 8: Optional Logging or Notification
– Add a **Slack** or **Email** node to send a summary upon completion
– Alternatively, use the n8n built-in **Write Binary File** node for a simple log
### Step 9: Activate Your Workflow
– Test your workflow on test tickets first
– Once verified, activate it
—
## Common Errors and Tips
– **Authentication failures**: Ensure your Jira API token and email are correct and API access is enabled.
– **Transition IDs unknown**: Use a GET transition node to dynamically fetch valid transition IDs.
– **Rate limiting**: Use the SplitInBatches node to process tickets serially and add delays if necessary.
– **Ticket statuses vary by project**: Modify the JQL and transition steps to align with your Jira workflows.
– **Error handling**: Add error workflows in n8n to catch and alert when a ticket cannot be processed.
—
## Scaling and Adaptation
– **Multi-project support**: Modify JQL to iterate over multiple projects by adding a loop or running multiple workflows.
– **Configurable stale period**: Use n8n workflow variables or credentials to customize the stale duration.
– **Enhanced logging and reporting**: Integrate with data warehouses or dashboards for periodic reporting on stale tickets and tickets closed.
– **Reopen flow**: Automatically reopen tickets if a user comments or interacts within a grace period.
—
## Summary
By leveraging n8n’s flexible workflow engine and Jira’s powerful API, operations teams can automate the tedious and error-prone task of maintaining a clean Jira backlog. This workflow identifies stale tickets based on inactivity, comments on them to inform stakeholders, and transitions them to closed status—all on a customizable schedule.
Automating these processes saves valuable developer time, reduces clutter, and improves project tracking accuracy. With slight tweaks, the workflow can scale across projects, adjust stale intervals, or integrate notifications, making backlog hygiene an efficient, hands-off process.
—
## Bonus Tip: Use Environment Variables and Credentials
Store sensitive information like Jira credentials in n8n Credentials for better security. Use Environment Variables to control parameters such as the stale period and project keys dynamically without modifying the workflow code.
—
### References
– [Jira Cloud REST API Documentation](https://developer.atlassian.com/cloud/jira/platform/rest/v3/intro/)
– [n8n Documentation](https://docs.n8n.io/)
– [Jira JQL Syntax](https://confluence.atlassian.com/jirasoftwarecloud/advanced-searching-764478330.html)