To quote a post I recently found resonating with me:
"Look, we don’t necessarily hang murderers to deter other people from committing the same offence. We kill them simply because the punishment has to carry the same weight as the offence. The family of the murderer must go through the same anguish and pain that the murder victim’s family went through. The killer has to be stopped from enjoying all the things that come with being alive. When you kill another person, you deprive them of worldly enjoyments like food, sex, conversations, bathing, laughing, crying and therefore it is only befitting that you too get deprived of same and the only way to do so is through the death sentence. If we are going to shy away from punishing wrong-doers on the basis that the punishment won’t stop other people from committing the same offence then we might as well not send anyone to jail because sending people to jail has never stopped other people from committing the same offences."
https://www.sundaystandard.info/iocom-a-retributionist-i-sup...
Or it's a form of societal self-defence: there are some folks that will continue to be a menace to society, whether they'll do murder or other bad things, and society wants to eliminate the risk/threat.
If someone comes at you with a knife or gun, you have a right to protect yourself, potentially up to the point of killing the attacker. If someone comes at a group of people (e.g., at a temple, mosque, church), they have a right to protect yourself, potentially up to the point of killing the attacker.
If someone keeps coming at member of society, society has a right to protect itself.
In modern times, with modern prisons, it is much easier to keep these people isolated from society at large, and so the need for the mechanism has been diminished, but the principle is still there.