n8n Loop Control: Prevent Infinite Loops with $runIndex

This workflow demonstrates using the $runIndex expression in an IF node to create a finite loop, posting 5 tweets saying 'Hello from n8n!' without risking infinite execution.

n8n
n8n Loop Control: Prevent Infinite Loops with $runIndex

This n8n workflow showcases a clever technique to implement controlled loops using the built-in $runIndex expression. Starting with a Manual Trigger, it enters a loop where a Twitter node posts 'Hello from n8n!', then feeds into an IF node that checks if $runIndex is less than 5 (configured as value1={{$runIndex}} compared to 4). If true, it loops back to Twitter; if false, it exits to a NoOp node, ensuring exactly 5 iterations.

The primary benefit is preventing infinite loops, a common pitfall in workflow automation. By leveraging $runIndex—which increments per execution in looped paths—developers can add simple, expression-based termination conditions without external counters or complex state management. This keeps workflows lightweight and efficient.

Ideal use cases include batch processing like posting multiple social media updates, sending a fixed number of emails or API calls, data scraping in controlled batches, or testing repetitive actions. Easily adapt by swapping Twitter for any node (e.g., Email, HTTP Request) and adjusting the IF threshold. With over 10,000 views, it's a proven educational gem for n8n users tackling iterative automations.

$8.99
Last updated August 29, 2026
30-day money-back guarantee
Instant download
Lifetime updates included

New buyers can create an account from the cart to unlock a controlled $10 first-purchase credit on eligible orders of $25+.

Secure checkout powered by Stripe

Support

How to import this workflow into n8n

  1. 1Purchase or download the workflow to get the n8n workflow JSON file.
  2. 2In your n8n instance, open Workflows and choose "Import from File" (or paste the JSON with Ctrl+V on the canvas).
  3. 3Open each node marked with a credential warning and connect your own accounts and API keys.
  4. 4Run the workflow once manually to verify the data flow, then toggle it to Active.

Related Human Resources workflows

More from Timo Salim

Need this deployed? We'll set it up for you.

Our automation experts deploy this workflow in your stack, connect your accounts, and verify it works — or build a custom solution from scratch.