There is an additional issue with this mod where the factory is visually in world but not selectable after being (I think?) destroyed in this way. I don't know how this interacts with your commands to get destroyed factories, it might mess with your state tracking.
This would mean that when tiles change, the main building entity is destroyed, but Factorissimo isn't receiving the proper entity death event. Not sure what I should do about this, except maybe add occasional checks for whether the main building entity is still alive.