fixes #3890 error while deleting container used by template

Fixed the issue and pushed code for review, please check it.