One takes all possible means so that food does not get spoiled in the first place, lest one fall into the vice of wastefulness (israf). [Nahlawi, Durar Mubaha]
If, despite taking those means, the food still gets spoiled, then it is best to give it to an animal (if reasonably possible) so that it is consumed and not thrown away.
If that is not reasonably possible though, such as with a broken egg, then there is nothing wrong with discarding it completely, as one really has no alternative.
May Allah protect us from being wasteful. Amin.
And Allah knows best.