Question: Is it permissible to kill harmful insects that cause minor harm, such as ants? They may multiply until vessels, food, and water are filled with them
The answer – and Allah is the One who grants success – is that it is permissible to get rid of any harmful animal, whether small or large. Permission has come (in the Sunnah) to kill the snake, the scorpion, the house lizard (wazagh), the kite, and the pied crow; by analogy, whatever is harmful among other animals falls under the same ruling.
It appears that killing lice was not disapproved in the time of the Prophet (May Allah bless him and his family and grant them peace) and the Companions. Had it been disapproved, that disapproval would have been transmitted. Rather, evidence has come that supports what we have said. It has been narrated that the Prophet (May Allah bless him and his family and grant them peace) saw Kaʿb ibn ʿUjrah during the ʿUmrah of al-Qaḍāʾ while he was in iḥrām and his head was swarming with lice. The Prophet (May Allah bless him and his family and grant them peace) said to him: “Are the vermin of your head harming you?” He replied: Yes. So the Prophet (May Allah bless him and his family and grant them peace) ordered him to shave his head and to offer a ransom of fasting, charity, or a sacrifice. Shaving the head in order to remove lice is a cause for destroying the lice, and the one who creates the cause is like the one who performs the resulting act, as shown by the story of the People of the Sabbath.
Source: Min Thimār al-ʿIlm wa al-Ḥikmah vol.2