Sign up for your free trial and begin automating your processes today.

How to Handle Nested Loops in n8n with Sub-workflows

Learn to handle complex nested loops in n8n. This guide shows you how to use sub-workflows for powerful, repeatable automations.

As you dive deeper into n8n, you’ll quickly realize the power of nodes like Loop for processing lists of data. But what happens when you need to handle more complex scenarios, like iterating over one list of data for every single item in another? This is the realm of nested loops — a fundamental concept in programming that requires a clever workaround in n8n.

In this guide, we’ll walk through a common problem with nested loops in n8n and reveal an elegant solution using sub-workflows.

The Initial Approach: A Tale of Two Loops

Let’s imagine a scenario where you have two distinct lists of data you need to combine: a list of colors and a list of numbers. Your goal is to create a new item for every possible combination of a color and a number.

A natural first instinct is to build a simple nested loop structure. You might start by creating a list of colors, perhaps in a Code node:

A screenshot of an n8n Code node, configured to return an array of three colors: yellow, blue and green.

Then, you would use a Loop node to iterate through each color.

A screenshot of an n8n Loop node configured to iterate over the list of colors from the previous node.

After executing the workflow, we can see all three colors appear in the Done Branch, which is exactly what we would expect at this stage.

A screenshot of the n8n Loop node’s Done Branch showing three output items, each representing one of the colors (yellow, blue, and green).

Inside that loop, you’d add another Code node with your numbers:

A screenshot of an n8n Code node, configured to return a list of three numbers: 1, 2, and 3.

Finally, you would add a nested Loop node to process the numbers, and a Set node at the very end to combine the data and see the results.

A screenshot of the n8n Set node’s parameters, showing expressions to combine the color and number values.

Your workflow might look something like this:

A diagram of an n8n workflow with a nested loop structure. The outer loop iterates over colors, and an inner loop iterates over numbers.

Now let’s execute the workflow and check the results. You will find something unexpected. The outer loop’s Done Branch might show a single item, or a number of items that don’t make sense, and the values are completely wrong.

A screenshot of the output of the nested loop workflow, showing an unexpected number of items (18) and incorrect data in the outer loop’s Done Branch.

This happens because n8n’s Loop node doesn’t operate like a traditional programming loop in this nested context. The inner loop executes only once on the initial data, rather than being “reset” for each item of the outer loop. This is a common point of confusion for new n8n users.

The Solution: Harnessing the Power of Sub-workflows

Fortunately, there is a simple and effective workaround that allows you to achieve the desired nested loop functionality: using an Execute Sub-workflow node.

A sub-workflow is a separate, self-contained workflow that can be triggered by another workflow. This is the key to our solution, because it allows you to create a new “execution context” for each item in the outer loop.

Step 1: Create the Inner Loop as a Sub-workflow

First, let’s build the inner loop logic in its own separate workflow. This new workflow will handle the iteration over the numbers. It will need a starting point to receive the data from the main workflow, which we’ll handle with an Execute Sub-workflow Trigger node.

  1. Create a new workflow.
  2. Add an Execute Sub-workflow Trigger node. Give it a descriptive parameter name, like color, which will hold the color from the outer loop.
  3. Add the Code node with your list of numbers, just as you did before.
  4. Add a Loop node to iterate over the numbers.
  5. Inside the Loop, add a Set node. Here, you’ll set the final output, combining the color (from the input trigger) and the number (from the current loop item).

A screenshot of the Execute Sub-workflow Trigger node’s parameters, showing the ‘color’ parameter being defined.

A screenshot of the Set node’s parameters, showing the expressions being used to combine the color and number values.

The final sub-workflow should look like this:

A screenshot of the final sub-workflow, showing the Execute Sub-workflow Trigger, Code, Loop, and Set nodes connected in a sequence.

Step 2: Call the Sub-workflow from Your Main Workflow

Now, let’s go back to your main workflow.

  1. Remove the nested Loop node you had before.
  2. In its place, add an Execute Sub-workflow node.
  3. Configure the Execute Sub-workflow node to call the sub-workflow you just created.
  4. Most importantly, you need to pass the current item from the main workflow’s loop into the sub-workflow. To do this, link the output of your first Loop node to the Execute Sub-workflow node. You’ll pass the color value into the color input parameter you defined in the sub-workflow.

A screenshot of the Execute Sub-workflow node’s parameters, showing how the ‘color’ value from the main workflow is passed to the sub-workflow.

Your main workflow will now be much cleaner:

A screenshot of the main workflow, showing the starting nodes connected to the Execute Sub-workflow node.

Step 3: Run and Verify

Now, when you execute the main workflow, you will see the results you were hoping for! For each color in your first list, the sub-workflow will be called, and it will iterate through all the numbers, creating 9 unique items (3 colors x 3 numbers).

A screenshot showing the final output of the sub-workflow solution, with 9 unique items created from the combinations of colors and numbers.

The Takeaway

While n8n doesn’t support nested loops in the traditional sense, using a sub-workflow is a powerful and reliable pattern. It allows you to create a modular, clean, and repeatable process for handling nested data structures. As you build more complex workflows, remember this trick—it’s an essential tool for an n8n learner!

n8n is a trademark of n8n GmbH. This site is not officially associated with n8n GmbH.
Built with Hugo
Theme Stack designed by Jimmy