AimsThere is evidence gastrointestinal (GI) motility may play a role in the development of GI cancers. Weak opioids (codeine and dihydrocodeine) decrease GI motility, but their effect on GI cancer risk has not been assessed. We aim to assess the association between weak opioids and cancers of the GI tract.MethodsA series of nested case‐control studies was conducted using Scottish general practice records from the Primary Care Clinical Informatics Unit Research database. Oesophageal (n = 2432), gastric (n = 1443) and colorectal cancer (n = 8750) cases, diagnosed between 1999 and 2011, were identified and matched with up to five controls. Weak opioid use was identified from prescribing records. Odds ratios (ORs) and 95% confidence intervals (CIs) were calculated using conditional logistic regression, adjusting for relevant comorbidities and medication use.ResultsThere was no association between weak opioids and colorectal cancer (adjusted OR = 0.96, CI 0.90, 1.02, P = 0.15). There was an increased risk of oesophageal (adjusted OR = 1.16, CI 1.04, 1.29, P = 0.01) and gastric cancer (adjusted OR = 1.26, CI 1.10, 1.45, P = 0.001). The associations for oesophageal cancer, but not gastric cancer, were attenuated when weak opioid users were compared with users of another analgesic (adjusted OR = 1.03 CI 0.86, 1.22, P = 0.76 and adjusted OR = 1.29 CI 1.02, 1.64, P = 0.04 respectively).ConclusionsIn this large population‐based study, there was no consistent evidence of an association between weak opioids and oesophageal or colorectal cancer risk, but a small increased risk of gastric cancer. Further investigation is required to determine whether this association is causal or reflects residual confounding or confounding by indication.