BACKGROUND: Type 2 diabetes elevates the risk of severe outcomes in COVID-19 patients, with multiple studies reporting higher case fatality rates. Metformin is a widely used medication for glycemic management. We hypothesize that improved adherence to metformin may lower COVID-19 post-infection mortality risk in this group. Utilizing data from the Mexican Social Security Institute (IMSS), we investigate the relationship between metformin adherence and mortality following COVID-19 infection in patients with chronic metformin prescriptions. METHODS: This is a retrospective cohort study consisting of 61,180 IMSS beneficiaries who received a positive polymerase chain reaction (PCR) or rapid test for SARS-CoV-2 and had at least two consecutive months of metformin prescriptions prior to the positive test. The hypothetical intervention is improved adherence to metformin, measured by proportion of days covered (PDC), with the comparison being the observed metformin adherence values. The primary outcome is all-cause mortality following COVID-19 infection. We defined the causal parameter using shift intervention, an example of modified treatment policies. We used the targeted learning framework for estimation of the target estimand. FINDINGS: Among COVID-19 positive patients with chronic metformin prescriptions, we found that a 5% and 10% absolute increase in metformin adherence is associated with a respective 0.26% (95% CI: -0.28%, 0.79%) and 1.26% (95% CI: 0.72%, 1.80%) absolute decrease in mortality risk. INTERPRETATION: Subject to the limitations of a real-world data study, our results indicate a causal association between improved metformin adherence and reduced COVID-19 post-infection mortality risk.