This is a corrected version of my deleted answer.
Magnetic material can be magnetized, i.e. all the tiny magnetic domains (in a ferromagnet for example) can become "permanently" oriented by using a strong external magnetic field. One then has a permanent magnet that has potential energy stored in the orientation, an ordered structure . Similar to a crystal structure , which is stable until external effects supply energy to destroy it .
During magnetization of a material, there is what is called a magnetocaloric effect, which shows that the process of creating a magnetization is exothermic.
The increasing external magnetic field (+H) causes the magnetic dipoles of the atoms to align, thereby decreasing the material's magnetic entropy and heat capacity. Since overall energy is not lost (yet) and therefore total entropy is not reduced (according to thermodynamic laws), the net result is that the substance is heated (T + ΔTad).
This article summarizes how one can demagnetize a magnet:
Demagnetize a Magnet by Heating or Hammering
If you heat a magnet past the temperature called the Curie point, the energy will free the magnetic dipoles from their ordered orientation
Heating and hammering can happen when the magnet is used to mechanically move objects, or in a dynamo, due to friction and induced magnetic effects in surrounding materials
......
Self Demagnetization
Over time, most magnets naturally lose strength as long range ordering is reduced. Some magnets don't last very long, while natural demagnetization is an extremely slow process for others.
This is due to thermodynamic processes as at any temperature in a solid there are vibrations and rotations which generate black body radiation and loss of orientation.
......
Apply AC Current To Demagnetize a Magnet
One way to make a magnet is by applying an electrical field (electromagnet), so it makes sense you can use alternating current to remove magnetism, too.
This reverses the process that magnetized the material.
Perpetual motion machines which use permanent magnets all can only work until the effects of demagnetization from friction, heating, and stray magnetic fields (if an attempt is made to extract work) , will demagnetize the permanent magnets.