To demagnetise a permanent magnet place it inside a solenoid, pointing East-West, through which an alternating current is flowing. While the current is still flowing, the small atomic magnets present in the permanent magnet get disoriented and hence the permanent magnet gets demagnetised.
AC current is passed through a solenoid. A higher current s chosen and slowly reduce it until it’s zero. Alternating current immediately switches directions, changing the orientation of the electromagnetic field. The magnetic dipoles try to orient according to the field, but since it’s changing, they conclude up randomized. The core of the material may preserve a slight magnetic field due to hysteresis.