How to reset $runIndex or manage loop-specific counters across multiple main items?

Describe the problem/error/question

Hi everyone,

I had opened an topic in this forum about how to make a counter inside of n8n. Now, I wanted to improve and make it more dynamic pipeline. So here is the question: I have a workflow where I process multiple links using a Loop Over Items node. Inside this loop, there is a sub-loop (polling an API) that uses an If Node to limit the number of attempts.

The Problem: I wanted to use {{ $runIndex }} or {{ $input.all().length }} as a counter to stop the polling after 10 attempts. However, these values are cumulative across the entire execution.

When the main loop moves to the second link, $runIndex doesn’t reset to zero. It starts from where it left off (e.g., 11, 12, 13…), which immediately triggers the “Limit reached” condition in my If Node, causing the second item to fail instantly.

My Questions:

  1. Is there any way to reset the $runIndex for a specific node when moving to the next item in a parent loop?

  2. If not, what is the best practice to implement a “per-item” counter that resets every time a new item enters the sub-loop without making the workflow too messy?

I tried using a Code Node to increment a counter variable, but I’m struggling with data persistence between the iterations of the polling loop.

Any advice would be great!

What is the error message (if any)?

N/A

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: v2.13.4
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Ubuntu 24.04

Hi @atakanozban :waving_hand:
I would move the polling logic into a child workflow and call it once per main item, because each child execution gets its own isolated state and avoids the execution-wide counter behavior in the parent workflow.

When I try as you said it’s just increases the input items like:

And the data doesn’t change.

Thank you for your comment.

That’s a good idea to put this into a child workflow. Once I successfully finish the pipeline, I’m planning to move every function into child workflows. As you know, it becomes very difficult to troubleshoot or perform maintenance when everything is in one place. However, I’m wondering if there is a Plan B or C that I could easily integrate into my current pipeline right now.

@atakanozban drop a Set node right before your sub-loop starts (after Loop Over Items2 gives you an item) that captures {{ $runIndex }} into a field called loopStart, then in your If node check {{ $runIndex - $('Save Start Index').first().json.loopStart }} < 10 instead of raw $runIndex — since that Set node only fires once per main item, the difference gives you a clean per-item counter

replace your Initialize Counter with “Save Start Index” and swap your existing If condition to use that subtraction expression, the rest of your workflow stays the same.

Thank you for your help :raising_hands:t2: