Hazrat Shaikh Moulana Muhammad Zakariyya (rahmatullahi ‘alaih) once mentioned in reply to a person who complained about stray thoughts:

The best cure for stray thoughts mentioned in the hadeeth is for one not to pay any attention to them. This cure has proven to be extremely effective in repelling stray thoughts. Stray thoughts were something that even the Sahaabah (radhiyallahu ‘anhum) experienced. However, when they would mention their condition to Rasulullah (sallallahu ‘alaihi wasallam), he would advise them not to pay any attention to the stray thoughts that cross their minds. Therefore, one should neither pay any attention to these stray thoughts that cross the mind nor should he show any importance to them.

(Qutbul Aqtaab Hazrat Shaikhul Hadeeth Moulana Muhammad Zakariyya (rahmatullahi ‘alaih) pg. 424)

Source: Ihyaauddeen.co.za