The epidemiological evidence regarding the correlation between air pollution, residential greenspace, and the risk of kidney stone disease (KSD) is limited, with no large-scale prospective studies conducted on this relationship. We conducted a large-scale prospective study from the UK Biobank to explore the correlation between air pollution, residential greenspace, and the risk of KSD. This study included 419,835 UK Biobank participants who did not have KSD at baseline. An air pollution score was derived through the summation of concentrations for five air pollutants, including particulate matter (PM) with aerodynamic diameter ≤2.5 μm (PM2.5), ranging from 2.5 to 10 μm (PM2.5-10), ≤10 μm (PM10), nitrogen dioxide (NO2), and nitrogen oxides (NOx). Various covariates were adjusted for in Cox proportional hazard regression to evaluate the risk of KSD associated with air pollution score, single air pollutant, and residential greenspace. During a follow-up period of 12.7 years, 4503 cases of KSD were diagnosed. Significant associations were found between KSD risk and air pollution score (HR: 1.08, 95% CI: 1.03-1.13), PM2.5 (1.06, 1.02-1.11), PM10 (1.04, 1.01-1.07), NO2 (1.09, 1.02-1.16), NOx (1.08, 1.02-1.11), greenspace buffered at 300 m (0.95, 0.91-0.99), and greenspace buffered at 1000 m (0.92, 0.86-0.98) increase per interquartile range (IQR). PM2.5 and NO2 reductions may be a key mechanism for the protective impact of residential greenspace on KSD (P for indirect path < 0.05). Prolonged exposure to air pollution was correlated with a higher risk of KSD, while residential greenspace exhibits an inverse association with KSD risk, partially mediated by the reduction in air pollutants concentrations. These findings emphasize the significance of mitigating air pollution and maintaining substantial greenspace exposure as preventive measures against KSD.