Rather than that if you want to be barbaric, just unhide all, zoom way out, click and drag around everything (selects everything with polygons/visible nodes), invert selection (gets your no-geometry nodes), delete. Ryno caveman style.
well that code snippet doesn't work apparently. i found the bug by individually selecting objects for baking, and got the exception when i selected the object with no mesh.