Sociality has evolved in a wide range of animal taxa but infectious diseases spread rapidly in populations of aggregated individuals, potentially negating the advantages of their social interactions. To disengage from the coevolutionary struggle with pathogens, some hosts have evolved various forms of "behavioral immunity"; yet, the effectiveness of such behaviors in controlling epizootics in the wild is untested. Here we show how one form of behavioral immunity (i.e., the aversion of diseased conspecifics) practiced by Caribbean spiny lobsters (Panulirus argus) when subject to the socially transmitted PaV1 virus, appears to have prevented an epizootic over a large seascape. We capitalized on a "natural experiment" in which a die-off of sponges in the Florida Keys (USA) resulted in a loss of shelters for juvenile lobsters over a ~2500km2 region. Lobsters were thus concentrated in the few remaining shelters, presumably increasing their exposure to the contagious virus. Despite this spatial reorganization of the population, viral prevalence in lobsters remained unchanged after the sponge die-off and for years thereafter. A field experiment in which we introduced either a healthy or PaV1-infected lobster into lobster aggregations in natural dens confirmed that spiny lobsters practice behavioral immunity. Healthy lobsters vacated dens occupied by PaV1-infected lobsters despite the scarcity of alternative shelters and the higher risk of predation they faced when searching for a new den. Simulations from a spatially-explicit, individual-based model confirmed our empirical results, demonstrating the efficacy of behavioral immunity in preventing epizootics in this system.