Only allow deleting of templates that are not being used

- While here, speed up template listing code

Ticket: #3733