Do you need to clear an array variable in Power Automate?
Variables are an essential feature that allows you to store and update values during the run time of your flow.
When utilizing a loop in Power Automate, storing values with an array variable is a practical way to keep track of each iteration. Once the loop concludes, you may want to use the results stored within the array for further actions in your flow.
However, you may also have the need to use the array for a different task within the same flow. For this, you will likely want to clear the array variable to be sure it’s empty before each use.
This article will show you how to reset an array variable and clear out any previous items stored in the array.
Initialize Array Variable
Clearing out an array will only work with an array variable. Other types of arrays you can create such as within a Compose action, can’t be cleared out.
You will first need to initialize an array variable using the Initialize variable action.
- Add the Initialize variable action to your flow.
- Give your variable a Name. This will be used to reference the variable later on in your flow when adding or updating items in the array.
- Select Array from the Type dropdown list to create an array type variable.
- Leave the Value field blank. This will create an empty array.
You can now use the initialized array variable to store and manage values in your flow.
Append to Array Variable In For Each Loop
It’s likely you will be using your array variable inside a For each loop.
You can use the Append to array variable action inside your to add items to your flow during each loop iteration.
- Add the Append to array variable action to your loop.
- Select the Name of your array variable that you previously initialized.
- Add the item you want to add to the array in the Value field.
You can also use the Append to array variable action outside of a loop, but this will only allow you to add one item to your array.
Now that you have some values in your array, you can remove them and reuse the array as if it were new!
Set Variable Action Outside Loop to Clear Array
To clear an array, you can use the Set variable action. This will ensure that the array is emptied. Here is the approach to use.
- Add the Set variable action to your flow whenever you want to clear the array. This might be outside your loop depending on your requirements.
- Select the Name of your array from the dropdown list.
- Set the Value of the variable to
[]
. This is an empty array or an array with no items.
This assigns an empty value to the array variable and effectively clears the array, removing all elements in it and initializing the variable with an empty array.
Conclusions
This article has shown you how to initialize your array variable, add items to it in a loop, and then clear the array.
This knowledge allows you to effectively manage your arrays, ensuring that you can reuse array variables in your flows.
To clear any array variable, you can use the Set variable action and set the value to an empty array.
Did you know how to clear the items from your array variables? Let me know in the comments!
Good article – but when I tried this method in a loop where I was adding to the array and clearing it at certain point using the method you have described and then later in that same loop when appending strings to it again, and then following that up with a compose action which would then display the content of the array in an email – we found that the first string in the array that was output was preceded by the open/close square bracket e.g.:
[]OutputString1
OutputString2
OutputString3
What did work to clear the array was rather than using [] we used the null expression.