Not sure I got what you meant to do here. Is this code just to avoid duplicates, like aliases with same name? If so I think we should handle this issue in the beginning, when creating the index template. The index templates already registered should be good right?