Drug–drug interaction (DDI) is common among the elderly, and it can have detrimental effects on patients. However, how DDI can be targeted has been under-researched. This study investigates whether DDI can be reduced by targeting continuity of care (COC) through reducing polypharmacy. Population claims data of Taiwan National Health Insurance were used to conduct a 7-year-long longitudinal study on patients aged ≥ 65 years (n = 2,318,766). Mediation analysis with counterfactual method and a 4-way decomposition of the effect of COC on DDI was conducted. Mediation effect through excessive polypharmacy differed from that through lower-level polypharmacy. Compared with the low COC group, the high COC group demonstrated reduced excess relative risk of DDI by 26% (excess relative risk = − 0.263; 95% Confidence Interval (CI) = − 0.263 to − 0.259) to 30% (excess relative risk = − 0.297; 95% CI = − 0.300 to − 0.295) with excessive polypharmacy as the mediator. The risk only reduced by 8% (excess relative risk = − 0.079; 95% CI, − 0.08 to − 0.078) to 10% (excess relative risk = − 0.096; 95% CI, − 0.097 to − 0.095) when the mediator was changed to lower-level polypharmacy. The effect of COC on DDI was mediated by polypharmacy, and the mediation effect was higher with excessive polypharmacy. Therefore, to reduce DDI in the elderly population, different policy interventions should be designed by considering polypharmacy levels to maximize the positive effect of COC on DDI.