This is more of a temp hack than a proper fix, proper solution would be to make shapekeys actual embedded IDs (which they are, in all aspects but actual implementation), and to address long-standing design tasks about handling of unused data on file save (see #61209 and #87490). But for now, simply do not write ShapeKeys IDs if they have no owner, or their owner has no user (and is therefore also not written to disk).