Insects are living things and are creations of Allah, so it is disliked (makruh) to kill them unnecessarily. However, if they are harming you or your things by getting into your food or your clothes then it is fine to kill them.
What the Scholars Said
A group of scholars said, “The relied upon position with regards to the killing of ants is that if they start to harm one (or one’s belongings), then there is nothing wrong in killing them, and if they do not harm, it is disliked to kill them. However, all the scholars agree that it is prohibited to throw ants in the water (since that tortures them).” (Al-Fatawa Al-Hindiyyah, 5:360-361)
Shaykh Mustafa Umar