Assalamu Alaikum
Yes, you can kill them if they are harmful.
Is it permissible for someone in a state of pilgrim sanctity (ihram) to kill an ant?
Answer:
[This answer was originally posted as a response to a student question in the course, The Reliance Explained -- Part 2.]
In the Name of Allah, Most Merciful and Compassionate
Scholars distinguish between harmless large ants (al-naml al-sulaymaniyy) and harmful small ants. The latter are permissible to kill--just as it is permissible for a pilgrim to kill other harmful animals, such as scorpions and mice--but the former are impermissible to kill because (a) they are harmless and (b) the Prophet (Allah bless him and give him peace) forbade the killing of harmless large ants. [1] [2]
And Allah Most High knows best.
Hamza.
[1] Sulayman al-Jamal, Hashiyat al-Jamal `ala Fath al-Wahhab, Bab: Ma haruma bi al-ihram
[2] Abu Dawud narrates that the Messenger of Allah (Allah bless him and give him peace) forbade the killing of four kinds of animals: ants, bees, hoopoes, and surad [h: a kind of bird]. Baghawi, Nawawi, and others understood "ants" here to exclusively mean harmless black ants, not other kinds of ants.