Adequate waste management is vital for the success of social life, because waste accumulation increases sanitary risks in dense societies. We explored why different leaf-cutting ants (LCA) species locate their waste in internal nest chambers or external piles, including ecological context and accounting for phylogenetic relations. We propose that waste location depends on whether the environmental conditions enhance or reduce the risk of infection. We obtained the geographical range, habitat and refuse location of LCA from published literature, and experimentally determined whether pathogens on ant waste survived to the high soil temperatures typical of xeric habitats. The habitat of the LCA determined waste location after phylogenetic correction: species with external waste piles mainly occur in xeric environments, whereas those with internal waste chambers mainly inhabit more humid habitats. The ancestral reconstruction suggests that dumping waste externally is less derived than digging waste nest chambers. Empirical results showed that high soil surface temperatures reduce pathogen prevalence from LCA waste. We proposed that LCA living in environments unfavourable for pathogens (i.e. xeric habitats) avoid digging costs by dumping the refuse above ground. Conversely, in environments suitable for pathogens, LCA species prevent the spread of diseases by storing waste underground, presumably, a behaviour that contributed to the colonization of humid habitats. These results highlight the adaptation of organisms to the hygienic challenges of social living, and illustrate how sanitary behaviours can result from a combination of evolutionary history and current environmental conditions.