It's for self defense, when they feel threaten by enemies. This defense mechanism is called autothysis, from the Greek words for "self" and "sacrifice." It's only been seen in ants and termites.