How to Clear/Reset Variables in Power Apps?

While working with Power Apps variables, you might need to clear or reset them. In this tutorial, I will explain how to reset variables in Power Apps.

You can use three types of variables in Power Apps, and I will show you how to clear each type of Power Apps variable.

  • Global Variables: These are accessible throughout the app and are created using the Set function.
  • Context Variables: These are specific to a Power Apps screen and are created using the UpdateContext function.
  • Collections: These are multi-row variables that can store tables of data, created using the Power Apps Collect function.

Reset Global Variables in Power Apps

Global variables are accessible from anywhere within the app. To clear a global variable in Power Apps, you set it to Blank().

Syntax:

Here is the syntax to reset a global variable in Power Apps.

Set(VariableName, Blank())

Example:

Suppose you have a global variable UserName that stores the user’s name. To clear this variable, you can use the following formula:

Set(UserName, Blank())

Clear Context Variables in Power Apps

Context variables are specific to a particular screen. They are cleared in a similar way to global variables but use the UpdateContext function.

Syntax:

Here is the syntax to reset a context variable in Power Apps.

UpdateContext({VariableName: Blank()})

Example:

Suppose you have a context variable IsVisible that controls the visibility of a button. To clear this variable, use the following formula:

UpdateContext({IsVisible: Blank()})

Clear Power Apps Collections

Collections can store multiple records and fields. To clear a collection in Power Apps, you use the Clear function.

Syntax:

Here is the syntax to clear a collection in Power Apps.

Clear(CollectionName)

Example:

Suppose you have a collection named ShoppingCart that stores items added to a shopping cart. To clear this collection, use the following formula:

Clear(ShoppingCart)

Check out How to Create a Custom Theme in Power Apps?

Use ClearCollect to Reset a Power Apps Collections

The ClearCollect function in Power Apps is a combination of Clear and Collect. It clears the existing collection and then adds new data to it.

Syntax:

Here is the syntax to reset a Power Apps collection using ClearCollect.

ClearCollect(CollectionName, NewData)

Example:

Let me show you an example.

To reset UserCollection with new data:

ClearCollect(UserCollection, [{Name: "John Doe", Age: 30}])

Reset Variables in Power Apps Examples

Now, let me show you some examples of resetting variables in Power Apps.

I built an app from scratch using Power Apps for all the examples below. You can follow along with me.

Reset Power Apps Global Variable

First, let us see how to reset a Power Apps global variable.

In the Power Apps screen, I added two buttons and one TextLabel.

  • Button1: Set User Name
  • Button2: Reset User Name
  • TextLabel: Display user name

In the first button’s OnSelect property, write the below formula to set the user name.

Set(UserName, "Bijay Kumar")

In the Text property of the TextLabel, write the formula below to display the user name on the screen.

Text=UserName

Write the formula below to reset the username in the second button’s OnSelect property.

Set(UserName, Blank())

You can see how to write the formula on the Reset button.

Reset Power Apps Global Variable

Now, when you preview the App, you can see the output in the screenshot below. When I click on the Reset User Name button, it resets the global variable. Here is the output you can see in the screenshot below:

power apps delete global variable
power apps empty global variable

I hope this example helps you to reset global variables in Power Apps.

Delete Context Variables in Power Apps

I will use the above example to reset context variables in Power Apps.

In the first button’s OnSelect property, write the below formula to set the user name.

UpdateContext({UserName: "Bijay Kumar"})

In the Text property of the TextLabel, write the formula below to display the user name on the screen.

Text=UserName

Write the formula below to reset the username in the second button’s OnSelect property.

UpdateContext({UserName: Blank()})

You can see how to write the formula on the Reset button.

power apps delete context variable

Now, let’s preview the app and see how to reset the context variable by clicking the reset user name button.

You can see the output in the screenshot below:

In the left-hand image, the button click sets the user name, and in the right-hand image, the reset button click resets the user name.

power apps reset context variable
powerapps clear context variable

This is how to reset a context variable in Power Apps.

Reset a Power Apps Collection

Now, let me show you an example of how to reset a Power Apps collection.

Here, I will use the same screen, but instead of using the TextLabel, I will use Power Apps gallery control.

In the first button’s OnSelect property, write the formula below to create the collection.

Collect(Orders, {OrderID: 1, ProductName: "Laptop", Quantity: 2})

In the Item property of the gallery control, write the formula below to display the collection on the screen.

Item=Orders

Write the formula below to reset the username in the second button’s OnSelect property.

Clear(Orders)

You can see how to write the formula on the Reset Collection button.

powerapps clear collection variable

Now, let us run the app and see the output. You can see that when the user clicks on the Reset Collection button, it resets the collection, which is why the gallery becomes blank. Below are the screenshots for your reference.

power apps reset collection
power apps clear collection

This is how to reset a collection variable in Power Apps.

Conclusion

As a Power Platform developer, you will get scenarios to clear or reset variables while working with the Power Apps app. In this tutorial, I explained:

  • How to clear global variables in Power Apps
  • How to reset a context variable in Power Apps
  • Clear a collection in Power Apps.

Do let me know in the comment below if all these examples help you.