Objective To examine the associations between stopping treatment with opioids, length of treatment, and death from overdose or suicide in the Veterans Health Administration. Design Observational evaluation. Setting Veterans Health Administration. Participants 1 394 102 patients in the Veterans Health Administration with an outpatient prescription for an opioid analgesic from fiscal year 2013 to the end of fiscal year 2014 (1 October 2012 to 30 September 2014). Main outcome measures A multivariable Cox non-proportional hazards regression model examined death from overdose or suicide, with the interaction of time varying opioid cessation by length of treatment (≤30, 31-90, 91-400, and >400 days) as the main covariates. Stopping treatment with opioids was measured as the time when a patient was estimated to have no prescription for opioids, up to the end of the next fiscal year (2014) or the patient's death. Results 2887 deaths from overdose or suicide were found. The incidence of stopping opioid treatment was 57.4% (n=799 668) overall, and based on length of opioid treatment was 32.0% (≤30 days), 8.7% (31-90 days), 22.7% (91-400 days), and 36.6% (>400 days). The interaction between stopping treatment with opioids and length of treatment was significant (P<0.001); stopping treatment was associated with an increased risk of death from overdose or suicide regardless of the length of treatment, with the risk increasing the longer patients were treated. Hazard ratios for patients who stopped opioid treatment (with reference values for all other covariates) were 1.67 (≤30 days), 2.80 (31-90 days), 3.95 (91-400 days), and 6.77 (>400 days). Descriptive life table data suggested that death rates for overdose or suicide increased immediately after starting or stopping treatment with opioids, with the incidence decreasing over about three to 12 months. Conclusions Patients were at greater risk of death from overdose or suicide after stopping opioid treatment, with an increase in the risk the longer patients had been treated before stopping. Descriptive data suggested that starting treatment with opioids was also a risk period. Strategies to mitigate the risk in these periods are not currently a focus of guidelines for long term use of opioids. The associations observed cannot be assumed to be causal; the context in which opioid prescriptions were started and stopped might contribute to risk and was not investigated. Safer prescribing of opioids should take a broader view on patient safety and mitigate the risk from the patient's perspective. Factors to address are those that place patients at risk for overdose or suicide after beginning and stopping opioid treatment, especially in the first three months.