The Clear function in Power Apps is used to remove all records from a collection. This function is useful when you want to reset a collection to an empty state, either to prepare it for new data or to clear it as part of an app's logic.
PowerApps – Clear() function Syntax
- CollectionName: The name of the collection you want to clear.
Notes:
- Confirmation: If clearing a collection removes important data, consider adding a confirmation step to prevent accidental data loss.
- Default State: After clearing a collection, you might want to set it back to a default state or reload it with initial values if necessary.
- Check for Empty Collection: Before performing operations that depend on the collection, check if the collection is empty to avoid errors or unexpected behavior.
Example:
If(CountRows(Products) > 0, Notify("Collection is not empty"), Notify("Collection is empty"))
- Resetting Data: Use the Clear function to reset collections when starting a new process or workflow, ensuring that old data does not interfere with new data.
- Form Submission: Clear temporary collections used to hold form data after the form is submitted to prepare for the next submission.
- Conditional Logic: Use the Clear function as part of conditional logic to dynamically reset collections based on user actions or other events.
PowerApps – Clear a Simple Collection
Suppose you have a collection named MyCollection that you want to clear.
- Create the Collection:
ClearCollect(MyCollection, [1, 2, 3, 4, 5]);
- Clear the Collection:
- Add a Button control.
- Set the OnSelect property of the Button to:
When you press the button, MyCollection will be cleared and will no longer contain any records.
PowerApps – Clear a Collection with Records
Suppose you have a collection named Products with records that you want to clear.
- Create the Collection:
ClearCollect(Products,
Table(
{ProductID: 1, ProductName: "Laptop", Price: 1000},
{ProductID: 2, ProductName: "Tablet", Price: 500},
{ProductID: 3, ProductName: "Monitor", Price: 300}
)
);
- Clear the Collection:
- Add a Button control.
- Set the OnSelect property of the Button to:
When you press the button, Products will be cleared, removing all records from the collection.
PowerApps – Clear Multiple Collections
You can clear multiple collections by calling the Clear function for each collection.
- Create Multiple Collections:
ClearCollect(Collection1, [1, 2, 3]);
ClearCollect(Collection2, ["A", "B", "C"]);
- Clear Both Collections:
- Add a Button control.
- Set the OnSelect property of the Button to:
Clear(Collection1);
Clear(Collection2)
When you press the button, both Collection1 and Collection2 will be cleared.