Why do suicide has been kept in natural selection? Since I've heard about evolution theory, I tend to think about new things with the help of this theory. While not being always accurate, I find the reasoning that makes this theory possible very interesting.

We can ask a question like Why do humans have 2 eyes? and answer hypothetically that it was more advantageous for creatures to have 2 eyes than having 1 or 3. We can imagine that having 1 eye wouldn't allow to see the depth and make the creature able to live even when losing 1 eye. This answer isn't correct but makes sense.

We can do the same with behavior. Why do we tend to be jealous? We can imagine that forcing your sexual partner to stay with you gives your more chances to spread your genes. I'm not saying that this is the only answer but that's one hypothetical answer that makes sense.

However, what about suicide? From what I have read, people tend to suicide when their amount of pain is higher than what they can support. I don't see any reason why creatures(or anything from bacteria to human) would be advantaged from committing suicide. Why didn't the natural selection keep the individuals that can endure anything since they have more chances to stay alive and spread their genes?
This assumes that tendency to suicide is hereditary. It seems that currently, the consensus is that suicide risk can be increased by environmental factors as well as by family history, so it may only be partially encoded in our genes.

For that part of the 'suicide genes' that does survive the natural selection, some of the possible explanations could be that people who commit suicide may have already had children, so their genes have been passed on to the next generation.

Another explanation could be, linked to what Yuval Noah Harari mentioned in "Sapiens". Hunter-gatherers were constantly on the move and it was not uncommon for them to kill or leave behind the elderly or individuals who would otherwise be a burden or slow down the tribe. It became a social norm, potentially to the point where the sick individuals would be ready to commit suicide themselves. The tribes which practised this were able to hunt and gather more, increasing their chances of survival.

All this is, however, a speculation, it is very hard to prove the evolutionary causes of suicides under scientific rigour, especially when running a controlled study is almost impossible. Even if we were able to run a controlled study, we'd most likely find that there is a large number of factors that allowed suicide genes to prevail.