Demagnetization is the process of removing or reducing the magnetism of a magnet. The standard methods include:
Heating the magnet to a high temperature (above its Curie point) and allowing it to cool while oriented in the east-west direction, so it is perpendicular to the Earth's magnetic field.
Hammering or dropping the magnet repeatedly while it is oriented in the east-west direction.
Passing a decreasing alternating current through a solenoid containing the magnet.
The key requirement is that the magnet must be oriented in the east-west direction during demagnetization. This ensures the Earth's magnetic field does not re-magnetize the bar as its domains are disrupted.
Heating a magnetic bar red hot and allowing it to cool in the east-west direction is a valid demagnetization method. Heating disrupts the alignment of magnetic domains, and cooling in the E-W orientation prevents re-alignment along the Earth's field.
Placing the bar in a solenoid alone does not demagnetize it - it would magnetize it. Stroking or hammering in the north-south direction would tend to magnetize the bar rather than demagnetize it, because the N-S orientation aligns with the Earth's magnetic field.