Learning algorithms can acquire knowledge of quantum states to erase them at optimal energy cost.Learning can be made fully reversible and has no fundamental energy cost itself.The energy cost of erasing quantum states is related to their complexity, entanglement, and magic.Efficient work extraction can be achieved based on learning.