They are kept forever as they are part of the file system.
As far as I understand, what you need to care about when you reinstall your system are the group and user ID of the owners, for them to match.
I presume that such situation might have been experienced by someone else in the forum, and it’s the same that happens when you have your /home in another partition and reinstall the OS in /.
My upfront idea to test would be focus on single-user groups I’d create, and for which I can control the GID, rather than on users and groups that are default for the system, present in a lot of files, and that may change at the distribution’s will, or that may be different from one distribution to another.