You aren't destroying the variable, you're destroying the Unit Group that the variable is keeping track of. A variable is just a container of data - not the data itself. This is very important to understand and remember.
So when you set a Unit Group variable like this:
-
Set VariableSet soul_reap_units = (Units in (Playable map area) matching ((Level of Legion Unit Class (Icon) for (Matching unit)) Equal to 1))
Here's what's essentially happening under the hood:
1) The game creates a brand new
Unit Group object. At this stage the
soul_reap_units variable is not involved at all.
2) The game proceeds to add units to this new Unit Group based on your given filter (units with the Legion Unit Class ability).
3) The game sets the
soul_reap_units variable as the container for this new Unit Group. You'd have no way of using the Unit Group without it.
Hopefully that makes sense. So when you run the Custom Script to destroy
soul_reap_units you're destroying what it's currently Set to, not the variable itself. In this case it will destroy the Unit Group object containing all of those Legion units. Afterwards, the
soul_reap_units variable will continue to exist, it simply contains nothing and is ready to be used again in the future. If this trigger ever runs again then the whole process will start over and repeat steps 1 -> 3.
Note that
Local variables are different since they are meant to be temporary. You need to set them to
null when you're finished with them (the same memory leak rules apply with destroying their contained Unit Groups beforehand). This
nulling marks them for "destruction" and the game will automatically clear them from memory at some point in the future. Note that this only applies to local variables that are
NOT Integers, Reals, Strings, and Booleans. Those four are your basic data types and do not need to be cleaned up.
Also, you don't have to do this:
-
Set VariableSet soul_reap_pn = 0
-
Set VariableSet soul_reap_hero = No unit
This kind of "resetting" falls under the category of micro-optimization and is a time waster with microscopic results. In other words, you won't notice a difference in performance with or without it.