BackgroundThe global surge of environmental pollution exacerbates health issues, disease incidence, and economic strain. In China, the increasing healthcare costs of the elderly population necessitate addressing this challenge as part of the “Healthy China” strategy. We explore the impact of environmental quality on elderly healthcare expenses.MethodsThis study devised a comprehensive environmental quality index for 30 Chinese provinces, excluding Tibet, which was correlated with medical expenses for individuals older than 60 years, using China Family Panel Studies (CFPS) data. Because the traditional econometric model cannot solve the endogeneity problem and the selection of instrumental variables is subjective, a new machine learning algorithm is adopted based on the traditional ordinary least squares (OLS) model and the fixed effect model to conduct causal analysis to ensure the reliability of the results. Finally, heterogeneity analysis was conducted based on the generalized random forest algorithm.ResultsSouthern provinces such as Jiangxi and Guangxi exhibited superior environmental qualities. A regional analysis revealed a gradient where environmental quality decreased from west to east and from south to north. Both conventional and machine learning methodologies underscored a pivotal finding: enhanced environmental qualities significantly curtail elderly healthcare expenses. A heterogeneity assessment revealed that such improvements predominantly benefit elderly people in the eastern and central regions, with marginal impacts in the west. For different groups, the improvement of environmental quality can significantly reduce the medical expenditure of people aged 60 to 75, with bedtime hours between 9 and 11 PM and a lower household income.ConclusionsThis study, employing machine learning and traditional models, demonstrates that enhancements in environmental quality significantly reduce medical costs for the elderly in China, especially in the eastern and central regions, and among demographics such as individuals aged 60–75 and low-income households. These findings underscore the potential of environmental policies to lower medical costs within the “Healthy China” initiative framework. However, the study’s scope is limited by the environmental quality index and the extent of data coverage, indicating a need for further research expansion.