What This Workflow Does
This template solves a critical problem in automation: preventing infinite loops that can crash systems and disrupt business operations. When building automations that process multiple items or repeat actions, it's easy to accidentally create a loop that runs forever, consuming server resources and causing failures.
The workflow demonstrates how to use n8n's $runIndex expression to add a smart exit condition. It creates a controlled loop that executes a specific number of times (like sending 5 notifications or processing 10 records) then safely stops. This approach gives you the power of repetition without the risk of runaway processes.
Businesses use this pattern for batch operations, scheduled tasks, and any automation where you need to repeat an action but maintain complete control over execution limits. It's the difference between an automation that works reliably in production versus one that might fail unpredictably.
How It Works
The template implements a simple yet powerful pattern that you can adapt to countless business scenarios.
Step 1: Initialize the Loop
The workflow starts with a trigger or manual execution. It sets up the initial conditions and prepares to enter the repetitive processing phase. Think of this as loading your customer list or preparing your batch of data.
Step 2: Check the Run Index
Before each iteration, the workflow evaluates the $runIndex expression. This built-in n8n variable automatically tracks how many times the current execution path has run. The conditional check compares this value against your maximum limit.
Step 3: Execute or Exit
If the run index is below your threshold, the workflow proceeds with the business logic—sending an email, updating a record, or processing data. If the threshold is reached, the workflow takes the exit path and completes successfully.
Step 4: Loop Back or Complete
After successful execution, the workflow either loops back for another iteration or proceeds to completion tasks like sending success notifications, logging results, or triggering downstream processes.
Who This Is For
This template is essential for any business using automation for repetitive tasks. Marketing teams running batch campaigns, operations teams processing daily transactions, support teams sending follow-up communications, and development teams managing data synchronization will all benefit from controlled loops.
If you've ever worried about an automation running too long or consuming too many API calls, this pattern provides the safety net you need. It's particularly valuable for businesses scaling their automation efforts where reliability becomes as important as functionality.
What You'll Need
- An n8n instance (cloud or self-hosted)
- Basic understanding of workflow concepts in n8n
- A specific use case involving repetitive actions
- Knowledge of your system's limits (API rate limits, processing capacity)
- Test environment to validate loop behavior before production
Pro tip: Always test loop limits with small numbers first. Start with 2-3 iterations to verify the exit condition works, then scale up to your production number. This prevents accidental resource consumption during development.
Quick Setup Guide
- Download the template using the button above
- Import the JSON file into your n8n instance
- Examine the IF node to understand the
$runIndexcondition - Replace the example action (like the Twitter node) with your own business logic
- Adjust the maximum loop count based on your needs
- Test with a small limit first, then deploy to production
Key Benefits
Prevents System Overload: Infinite loops can crash servers and disrupt operations. This template ensures your automations respect resource limits and complete predictably.
Maintains API Compliance: Many services have rate limits. Controlled loops help you stay within these limits while still processing the data you need.
Enables Batch Processing: Process large datasets in manageable chunks without manual intervention. The automation handles the repetition while you focus on results.
Improves Reliability: Automations with proper exit conditions are more trustworthy in production environments. Teams can deploy them with confidence knowing they won't run amok.
Saves Development Time: Instead of building custom loop logic for every automation, use this proven pattern as a starting point for any repetitive task.