Our understanding of the response of macrofauna diversity patterns to the variability of sandy beaches across spatial scales is limited. Defining relationships between diversity and ecosystem productivity is key to understanding the ecological consequences of the current global rates of biodiversity loss. Here, we conducted a study across a large spatial gradient of 39 sandy beaches involving a wide range of environmental conditions and macrobenthic diversity to (1) explore macrofauna diversity patterns (2) estimate secondary production and (3) quantify how much of the variability in beach secondary production can be explained by macrofauna diversity. Beach macrofauna showed a clear increase in α-diversity across a beach geographic gradient linked to oceanographic conditions. Partitioning of β-diversity implied the replacement of some species by others between beaches (i.e. spatial turnover) instead of a process of species loss (or gain) from beach to beach (i.e. nestedness). Variance partitioning analyses revealed that environmental and oceanographic variables (i.e., sea surface temperature, beach size, slope, and exposure rate), but also macrofauna diversity (i.e., species richness and Shannon index), largely determine beach secondary production. We showed that an increase in macrofauna diversity enhances beach secondary production, promoting energy transfer across trophic levels. The positive exponential relationship between macrofauna diversity and secondary production supports the idea that macrofauna plays an essential role in maintaining beaches as productive coastal ecosystems. Consequently, macrofauna diversity loss due to the ongoing shoreline recession and coastal occupation, exacerbated by climate change might cause exponential reductions in beach secondary production, which would affect the functioning of these sea-land interface areas.