It is narrated from Sayyiduna Abu Hurairah (may Allah be pleased with him) that the Messenger of Allah (peace and blessings be upon him) ordered the killing of two black things during prayer. The narrator of the hadith, Yahya bin Sa'id, said: By «أسودان» is meant: the snake and the scorpion.
Hadith Referenceسنن دارمي / من كتاب الصللاة / 1543
The command to kill the two black creatures even during prayer is given because the black snake and the black scorpion are more venomous. Killing them is very necessary; otherwise, they will cause harm. Therefore, it is permissible to kill a harmful creature before it causes harm. Furthermore, killing a snake or a scorpion does not invalidate the prayer, provided that no other act is committed which would nullify the prayer.