Photon shot noise is sqrt(N) though, so even after the subtraction you're left with more noise along that line than elsewhere. So that's not ideal. But increased noise is less of a concern that "totally ruined data".
Another caveat is that the lines have to not be so bright as to saturate your CCD, otherwise you truly have lost data that subtraction won't help with.
Edit: it turns out astronomers take many shorter exposures [1] and simply reject the pixels that are too far away from the mean in any given exposure when combining them. This seems mostly better than what I was proposing, though it does mean increased readout noise since you read out multiple times and so for very sensitive measurements I would still expect astronomers might want to do true single long exposures.
[1] https://www.skyandtelescope.com/astronomy-blogs/imaging-foun...