I also had to modify this line into 
```
templates_clear = exist_templates_ids.difference(template_ids)
```
and add 
```template_ids_list = list(template_ids)```
to avoid issues where duplicate template id's where passed in