It’s about keeping a node with specific name, when you can scan these nodes and check if they are in instance or not to mark it as detached.
Check youtube video " Using Figma to go beyond Figma - Dylan Babbs, Vietanh Nguyen (Config 2021)" (can’t use links in the forum )
We have a similar way, but a bit more advanced