Fixed tag handling when files are deleted

Previously all tags associated with deleted files were accidentally deleted. Now
only those tags that do not have associations with any other files are deleted.