Abu Hurairah reported that the Prophet Muhammad (peace and blessings be upon him) said, “Kill the two black ones during prayer, the snake and the scorpion.” (Sunan Abu Dawud, Al-Tirmidhi and al-Nasa’i)

In view of this narration, if a person kills a harmful insect while praying without moving more than what is regarded as acceptable, then this is okay and the prayer is still valid.