in reply to
Generate a unique ID
As you further describe this as being related to “spill files,” if it is possible to do so (and if this is not already what you are doing), it seems to me that it would be a good idea to set up a randomly-named “spill directory.” Put all of the spill-files, no matter what they may be named, into that. When the process ends, it likewise tries to clean up “such-and-such directory, no matter what it contains.”
This might be a much less pervasive change ... requiring less code in the various programs to be considered and touched as you are implementing this change. You no longer care about the name of the toys upon the floor; only the name of the playpen.