In the first place we would like to make it clear that killing harmful insects is Islamically permissible, even recommended, to avoid the harm they do. The Prophet (peace and blessings be upon him) declared it permissible to kill such insects even in Al-Haram (the Holy Mosque) of Makkah where hunting is originally impermissible.
Sheikh Muhammad Saleh Al-Munnajid, a prominent Saudi scholar and lecturer states that: “Harmful insects may be killed with insecticides [or other means, but not by fire]. The Messenger (peace and blessings of Allah be upon him) said: ‘There are five pests that may be killed whether one is in ihram (state of consecration) or not: rats, scorpions, crows, kites [a hawk-like bird], and mad dogs.’ (Reported by Muslim). Another report adds a sixth, snakes. Thus the Prophet (peace and blessings of Allah be upon him) told us that they are harmful pests, and that they are unlike other animals which do not do harm, and so he told us to kill them whether we are in ihram or not. If other creatures such as ants, cockroaches or dung-beetles cause harm, they may also be killed with insecticides [or other means, but not by fire].”
Also, in the Kuwaiti Encyclopedia of Islamic Jurisprudence, we read:
“Generally speaking, it is recommended to kill every harmful insects and crawling thing [the Arabic word hasharat includes both insects and arachnids], like scorpions, lice, mosquitoes, or fleas.
The Malikis hold the view that it is only permissible to kill such insects because of the Hadith in which the Prophet (peace and blessings be upon him) was asked about insects and he said: ‘Whatever causes you harm, you can harm it before it harms you.’
The Hanbalis say that it is recommended to kill whatever insects [and crawling things] are harmful. According to them, it is recommended to kill lice, mosquitoes, fleas, flies and the like. As for the non-harmful insects, some say that it is disliked to kill them, while some others say it is forbidden to kill them.
As for killing ants, it is disliked to kill them unless they are really harmful, in which case it becomes permissible to kill them.” Based on the above, if these insects are really harmful, then there is nothing wrong in killing them.