Share via

How to break ForEach loop in ADF

Fasd 0 Reputation points
2026-05-05T15:10:56.3066667+00:00

Hi everyone,

I have a ForEach activity containing a Copy Data task.

The Problem:

By default, if one iteration fails, the ForEach continues to trigger all other iterations. In my case, if the first one fails, the following ones are guaranteed to fail as well. I want to "exit" or "break" the loop immediately upon the first error to avoid unnecessary activity runs.

My current workaround:

To solve this, I'm using an If Condition inside the ForEach:

I check a variable status.

If status == 'Success', I run the Copy Data.

If the copy fails (red path), I set the status to 'Failed'.

This prevents the actual copy from running in subsequent iterations, but the ForEach still technically "runs" every item in the list.

My Question:

Is there a more "native" or "clean" way to stop a ForEach loop immediately when an error occurs? I'm looking to:

Avoid wrapping everything in an If Condition.

Physically stop the loop so it doesn't even "check" the remaining items.

Is there a setting I missed, or is switching to an Until activity the only true solution for a "hard break"?

Thanks!

Azure Data Factory
Azure Data Factory

An Azure service for ingesting, preparing, and transforming data at scale.


1 answer

Sort by: Most helpful
  1. Vinodh247 42,206 Reputation points MVP Volunteer Moderator
    2026-05-05T15:46:38.5733333+00:00

    Hi ,

    Thanks for reaching out to Microsoft Q&A.

    there is no native “break” or “fail-fast” option in a ForEach activity in Azure Data Factory or Fabric pipelines.

    ForEach is designed to be resilient and independent per iteration, so even on failure it will continue scheduling remaining items (especially in parallel mode). What you are doing with a status variable + If Condition is the standard workaround, but as you observed, it does not truly stop execution, it only skips logic.

    If you need a true “hard stop” (stop further iterations from even triggering), the only clean pattern today is to replace ForEach with an Until loop where you control the index and exit condition explicitly. In that design, you can fail fast and break the loop immediately when an error occurs. Another partial option is to set ForEach to sequential (batch count = 1) and let the pipeline fail on first error, but even then it does not behave like a strict break control.

    Bottom line: no native break; Until is the only proper fail-fast pattern.

     

    Please 'Upvote'(Thumbs-up) and 'Accept' as answer if the reply was helpful. This will be benefitting other community members who face the same issue.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.