How can I empty an array variable in Azure Logic Apps?

Z.K.Z 221 Reputation points
2022-04-30T19:12:55.127+00:00

Hello,

I have create a For Each Loop in Azure Logic Apps. Before, I initialized an array variable and I want to write different infos into it with each iteration. For each new iteration I would like to clear the array variable. For example we have 3 iterations. For the first one I want to get ["red", "blue", "green"], emptying array variable, next ["yellow", "blue", "red"], emptying array variable again and for third ["blue","yellow","orange"].

I have tried to use Set variable in the For each activity with the following values:

array('')
array([])

but each time I get a result like this ["", "yellow" "blue", "red"], ["","blue","yellow","orange"]

Any suggestions how I can clear the array variable without the "" at the start?

Thanks and best regards

Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,470 questions
0 comments No comments
{count} vote

Accepted answer
  1. Bruno Lucas 4,436 Reputation points MVP
    2022-05-02T02:25:14.41+00:00

    Hi, you may not need to do that depending how you arrange the flow, but if you want to try clearing an array, click on the "set value" of "set variable" and switch to "expression", type null and click ok. Another aproach is creating a second array variable, keep it empty and assign the original array to the value of the second array: https://powerusers.microsoft.com/t5/Building-Flows/Clear-array-variable-in-a-flow/m-p/82097#M8153

    198131-image001.png

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Balint toth 5 Reputation points Microsoft Employee
    2025-03-31T19:55:34.1466667+00:00

    i had similar issue because of the default degree of paralellism in the foreach loop. After setting this to no paralellism, the issue was sorted

    User's image


  2. Sonny Gillissen 3,596 Reputation points
    2025-03-31T20:43:06.96+00:00

    Hi Z.K.Z

    Thanks for reaching out on Microsoft Q&A!

    To clear the array variable at each iteration is to add a “set variable” action as first action within the foreach loop, select the array variable and add [] to the value. This way the array is basicallly reset back to an empty array (which is different than null as this entirely removes any value, also the empty array).

    Due to the paralism and the nature of logic apps having global variables only you might want to consider avoiding variables in foreach loops (as values can get mixed up). You can substitute the variable with a “compose” or “select” action, living in the context of the foreach loop.

    Please click ‘Accept answer’ if you think my answer is helpful. Feel free to drop additional queries in the comments below!

    Kind regards,

    Sonny

    0 comments No comments

Your answer