Background: Peptic ulcers in patients receiving aspirin are associated with Helicobacter pylori infection. We aimed to investigate whether H pylori eradication would protect against aspirin-associated ulcer bleeding. Methods: We conducted a randomised, double-blind, placebo-controlled trial (Helicobacter Eradication Aspirin Trial [HEAT]) at 1208 primary care centres in the UK, using routinely collected clinical data. Eligible patients were aged 60 years or older who were receiving aspirin at a daily dose of 325 mg or less (with four or more 28-day prescriptions in the past year) and had a positive C13 urea breath test for H pylori at screening. Patients receiving ulcerogenic or gastroprotective medication were excluded. Participants were randomly assigned (1:1) to receive either a combination of oral clarithromycin 500 mg, metronidazole 400 mg, and lansoprazole 30 mg (active eradication), or oral placebo (control), twice daily for 1 week. Participants, their general practitioners and health-care providers, and the research nurses, trial team, adjudication committee, and analysis team were all masked to group allocation throughout the trial. Follow-up was by scrutiny of electronic data in primary and secondary care. The primary outcome was time to hospitalisation or death due to definite or probable peptic ulcer bleeding, and was analysed by Cox proportional hazards methods in the intention-to-treat population. This trial is registered with EudraCT, 2011-003425-96. Findings: Between Sept 14, 2012, and Nov 22, 2017, 30 166 patients had breath testing for H pylori, 5367 had a positive result, and 5352 were randomly assigned to receive active eradication (n=2677) or placebo (n=2675) and were followed up for a median of 5·0 years (IQR 3·9–6·4). Analysis of the primary outcome showed a significant departure from proportional hazards assumptions (p=0·0068), requiring analysis over separate time periods. There was a significant reduction in incidence of the primary outcome in the active eradication group in the first 2·5 years of follow-up compared with the control group (six episodes adjudicated as definite or probable peptic ulcer bleeds, rate 0·92 [95% CI 0·41–2·04] per 1000 person-years vs 17 episodes, rate 2·61 [1·62–4·19] per 1000 person-years; hazard ratio [HR] 0·35 [95% CI 0·14–0·89]; p=0·028). This advantage remained significant after adjusting for the competing risk of death (p=0·028) but was lost with longer follow-up (HR 1·31 [95% CI 0·55–3·11] in the period after the first 2·5 years; p=0·54). Reports of adverse events were actively solicited; taste disturbance was the most common event (787 patients). Interpretation: H pylori eradication protects against aspirin-associated peptic ulcer bleeding, but this might not be sustained in the long term. Funding: National Institute for Health and Care Research Health Technology Assessment.