I am trying to, by script, delete many tags on my production gateway (>500 tags). While I’m doing this I’d like to leave a few (<10 tags) in place because their history is being plotted, and I don’t want to lose it.
I’ve gone through and deleted all the tags I don’t want on my development gateway, leaving only the tags I want to be preserved. I copied the JSON representation of the top-level folder to a file. I thought the following script would remove all the tags I don’t want (that aren’t present in the JSON string). It isn’t. Can anyone help me understand why?
asdf = [
tagExport = system.file.readFileAsString(params)
for i in asdf:
The script will add tags that are missing, but it won’t remove the extra tags like I thought the Override collision policy would.
Veering slightly off-topic: I could delete the whole folder then re-import my configuration which I’m pretty confident would leave me with only the tags I want, but I’m concerned that I would lose the history when I delete the tags. Does anyone know if this is true?
Perhaps try using system.tag.deleteTags? You could get a list of the 10ish tag paths that you want left, then browse all the tags and delete any tag whose tag path isn’t on the list.
Simply deleting the root and importing the config would be the easiest option, but I’m not completely sure whether that messes with the id columns in the historian. I’d imagine it would be fine because the tag path would stay the same…
For the future:
I still don’t know why the overwrite collision policy doesn’t delete tags that aren’t present in the provided configuration, but this script accomplished my goal. I’m sure this could be improved in any number of ways, but it may serve as a starting point for someone else.
If you’re going to use this for anything, please test it thoroughly.
def findTagsToDelete(configuration, preserveTagPaths, pathRoot='', currentDepth=0):
# Need to keep track of the whole path of the tag.
concatChar = '/'
concatChar = ''
# Recursion scares me so I like to put checks in place.
# If you've got more than 15 levels of nested folders you may need to change this
maxDepth = 15
print 'Maxiumum Depth Reached'
# If tag is a folder, and it has tags, recurse for each child tag.
# If tag is a folder and it has not tags, delete the folder.
for i in configuration['tags']:
moreTagsToDelete = findTagsToDelete(i,preserveTagPaths,pathRoot,currentDepth)
# If tag is not a folder, determine if it it in the list of tags to be preserved.
# If not in the preserve list, add it to the delete list.
if pathRoot not in preserveTagPaths:
# tags I want to preserve.
preserveTags = [
# perform multiple passes because deleting this many tags will create empty folders
# these folders will be deleted on subsequent passes. There is definitely a more elegant
# way to accomplish this, but it's wicked fast and serves my need today.
dcsVibmonTags = system.tag.getConfiguration('folderToOverwrite',1)
asdf = findTagsToDelete(dcsVibmonTags, preserveTags)
Disclaimer: I’m not an expert on the tag system
I think this is because the collision policies are considered with individual tags, rather than being defined by particular folders. Folders are basically created implicitly (by virtue of tag(s) being defined with a parent path), rather than being explicit nodes to have their own collision rules. So the collision policy you choose just doesn’t really affect things here.
Couldn’t you just copy/paste your development tags to the prod ? Or import ?
Maybe. The tags already exist in production and I want to remove most of them. Someone went a little crazy dragging folders in from the OPC browser, and we don’t use most of the tags that are in the tree, so I’m house-cleaning (to the tune of 1500 tags all subscribed at 1 second).
Of the tags I’m keeping, some are tracked by the historian, and I don’t want those tags to be retired in the historian system because I want the trends to continue uninterrupted. Maybe I could have deleted all the tags and imported the ones I want to keep, but I wasn’t sure how that would impact historian continuity. I assume that trying to import tags would have encountered the same problem as system.tag.configure (you can add but not delete tags), but I admit I didn’t try it.