# Clearing an Array

**URL:** <https://forum.inductiveautomation.com/t/clearing-an-array/41745>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [December 23, 2020, 12:41am UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745 "2020-12-23T00:41:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![beter.dev](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/beter.dev/32/19903_2.png) [@beter.dev](https://forum.inductiveautomation.com/u/beter.dev)\
**Post date:** [December 23, 2020, 12:41am UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745/1 "2020-12-23T00:41:24Z")

</div>

Hi Guys,

I have a table in a view and I bind its Data property via a Tag History binding using an expression. The expression simply returns an array which I have as a custom property on the table.

In the table startup event, I construct an array which I write to the custom property I have on the table. Problem I’m having is this array builds up, that is it contains the last data I wrote to it along with the new data every time I launch it. What I want to do is clear the array every time the component launches before writing fresh data to it.

Any ideas?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [December 23, 2020, 1:12am UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745/2 "2020-12-23T01:12:57Z")

</div>

Can you post your startup script?

---

<div class="post-metadata">

**Author:** ![beter.dev](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/beter.dev/32/19903_2.png) [@beter.dev](https://forum.inductiveautomation.com/u/beter.dev)\
**Post date:** [December 23, 2020, 1:22am UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745/3 "2020-12-23T01:22:54Z")

</div>

Sure, here it is. Basically all I’m doing is getting all the tags under a path to show Tag History for them. The path is passed in by different devices.

```auto
def runAction(self):
	"""
	Method that will run whenever the selected event fires.

	Arguments:
		self: A reference to the component that is invoking this function.
	"""
	devicePath = self.view.params.basePath
	items = []
	allTags = []
	allTags = tags.browseTags(devicePath,filter = {'tagType':'AtomicTag'})
	for itag in allTags:
	    items.append({'path':itag,'alias':itag,'aggregate':'LastValue'})
	self.custom.key = items

```

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [December 23, 2020, 1:25am UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745/4 "2020-12-23T01:25:59Z")

</div>

Hmm, it looks like you’re already constructing the array from blank and is replacing the existing custom property value. Are you seeing that the custom.key property value keeps its old items from the previous run and your new items are being appended onto it? Or am i misunderstanding your question?

---

<div class="post-metadata">

**Author:** ![beter.dev](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/beter.dev/32/19903_2.png) [@beter.dev](https://forum.inductiveautomation.com/u/beter.dev)\
**Post date:** [December 23, 2020, 1:33am UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745/5 "2020-12-23T01:33:39Z")

</div>

Yes that is exactly what it is doing. It keeps the results from the previous run and then adds the new results so basically it’s doubling in length every time. I assumed if you set it to a new value to it it would overwrite the old contents but it doesn’t. I even tried adding the line custom.key = [] at the beginning of the script to see if that would zero it out everytime the script ran.

---

<div class="post-metadata">

**Author:** ![beter.dev](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/beter.dev/32/19903_2.png) [@beter.dev](https://forum.inductiveautomation.com/u/beter.dev)\
**Post date:** [December 23, 2020, 3:07am UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745/6 "2020-12-23T03:07:30Z")

</div>

Ok it’s officially driven me up the wall 😀

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [December 23, 2020, 4:43am UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745/7 "2020-12-23T04:43:42Z")

</div>

Are we 100% certain that the `tags.browseTags` invocation is not the cause? I just tried this locally using `system.tag.browse()` and everything worked fine. I suspect that project library is not clearing out an internal array and is then supplying the duplicated array. You could verify this if you print the length of the `allTags` variable immediately after it’s been assigned a value.

---

<div class="post-metadata">

**Author:** ![beter.dev](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/beter.dev/32/19903_2.png) [@beter.dev](https://forum.inductiveautomation.com/u/beter.dev)\
**Post date:** [December 23, 2020, 4:51am UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745/8 "2020-12-23T04:51:38Z")

</div>

Not sure, I took that code off the User Manual for Recursive browse and simply added a list to return. The code is below. Granted I couldn’t for the life of me work out how to return the results from the recursive browse function listed in the manual so I copied off someone who posted it in the forum 😌

```auto
def browseTags(path, filter, rtResult=[]):
  
    # First, browse for anything that can have children (Folders and UDTs, generally)
    results = system.tag.browse(path)
    for branch in results.getResults():    
        if branch['hasChildren']:
            # If something has a child node, then call this function again so we can search deeper.
            # Include the filter, so newer instances of this call will have the same filter.
            browseTags(branch['fullPath'], filter)
 
     
    # Call this function again at the current path, but apply the filter.
    results = system.tag.browse(path, filter)
    for result in results.getResults():
         
        # Here's where you'd want to do something more useful. For now we print.
        #print result['fullPath']
        if str(result['fullPath']).find('_types') == -1:
        	rtResult.append(result['fullPath'])
                    
    return rtResult

```

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [December 23, 2020, 9:19am UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745/9 "2020-12-23T09:19:29Z")

</div>

Yeah, that function doesn’t work well and _does_ append onto itself due to Python stuff…  
Replace this:  
`def browseTags(path, filter, rtResult=[]):`  
with this

```auto
def browseTags(path, filter, rtResult=None):
    if rtResult is None: rtResult = []

```

Basically, if you define a mutable object as the default argument in a function and you mutate it in the function, then Python keeps this in memory and doesn’t wipe it on the next function call.

---

<div class="post-metadata">

**Author:** ![beter.dev](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/beter.dev/32/19903_2.png) [@beter.dev](https://forum.inductiveautomation.com/u/beter.dev)\
**Post date:** [December 23, 2020, 11:34am UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745/10 "2020-12-23T11:34:44Z")

</div>

Genius, that did the trick. Just to add to that, in order to make it work for a recursive function I had to explicitly set rtResult in the subsequent call.

```auto
for branch in results.getResults():    
        if branch['hasChildren']:
            # If something has a child node, then call this function again so we can search deeper.
            # Include the filter, so newer instances of this call will have the same filter.
            browseTags(branch['fullPath'], filter, rtResult) <----

```

I must say, this should be made much easier to do. Hopefully down the track you can simply pass in a path with a wildcard and retrieve all the tags beneath that path.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [December 23, 2020, 7:34pm UTC](https://forum.inductiveautomation.com/t/clearing-an-array/41745/11 "2020-12-23T19:34:51Z")

</div>

The old tag browse function in 7 had it, and the recursive argument is actually still there in the new v8 function, but it has some issues currently so it’s currently undocumented. It’ll be back at some stage I assume
