Opioid dependence is associated with an increased risk of suicide. Drug-related mortality among people with opioid dependence in Scotland has more than tripled since 2010; less is known about changes in suicide risk. We aimed to determine if opioid agonist therapy (OAT) in Scotland is protective against suicide and to measure trends in suicide rates in those with opioid dependence over time. Retrospective cohort study. Scotland, UK. 46 453 individuals in Scotland who received at least one prescription for OAT between 2011 and 2020 with over 304 000 person-years (pys) of follow-up. We calculated standardised mortality ratios (SMR) using the age- and sex-specific suicide rates in Scotland for years 2011-2020. We fitted multivariable competing-risk regression models to estimate suicide rates by OAT exposure and to estimate trends over time, adjusting for potential confounders. There were 575 deaths classed as suicide among the cohort and the overall suicide rate was 1.89 (95% confidence interval [CI] = 1.74-2.05) per 1000 pys. Age and sex SMR for suicide was 7.05 times (95% CI = 6.50-7.65) higher than in the general population. After adjustment, OAT was shown to be highly protective against suicide, with rates more than three times greater (adjusted hazard ratio: 3.07; 95% CI = 2.60-3.62) off OAT compared with on OAT. Suicide rates decreased over time, falling from 2.57 (95% CI = 2.19-3.02) per 1000 pys in 2011-12 to 1.48 (95% CI = 1.21-1.82) in 2019-20. People with opioid dependence in Scotland appear to have a greater risk of suicide than the general population. Treatment is protective, with rates of suicide lower among those on opioid agonist therapy. Suicide rates have decreased over time, during a period in which drug-related death rates in Scotland have risen to globally high levels.