Cooperation fundamentally contributes to the success of life on earth, but its persistence in diverse communities remains a riddle, as selfish phenotypes rapidly evolve and may spread until disrupting cooperation. Here we investigate how evolutionary history affects the emergence and spread of defectors in multispecies communities. We set up bacterial communities of varying diversity and phylogenetic relatedness and measure investment into cooperation (proteolytic activity) and their vulnerability to invasion by defectors. We show that evolutionary relationships predict the stability of cooperation: phylogenetically diverse communities are rapidly invaded by spontaneous signal-blind mutants (ignoring signals regulating cooperation), while cooperation is stable in closely related ones. Maintenance of cooperation is controlled by antagonism against defectors: cooperators inhibit phylogenetically related defectors, but not distant ones. This kin-dependent inhibition links phylogenetic diversity and evolutionary dynamics and thus provides a robust mechanistic predictor for the persistence of cooperation in natural communities.