I remember there was a Cleanup function that worked more or less as you described. But was removed it seems: