We linked UK cancer registry data with the corresponding electronic primary care medical records of 6080 patients who died of cancer over a 7-year period in a large United Kingdom city. We extracted all prescriptions for analgesics issued to each patient in the linked cohort during the 12 months before death and analysed the extent and duration of strong opioid treatment with clinical and patient characteristics. Strong opioids were prescribed for 48% of patients in the last year of life. Median interval between first prescription of a strong opioid and death was 9 weeks (interquartile range 3-23). Strong opioid prescribing was not influenced by cancer type, duration of illness, or gender but was adversely influenced by older age. Compared with patients who died in a hospice, those who died in a hospital were 60% less likely to receive a strong opioid in primary care before admission (relative risk ratio 0.4, CI 0.3-0.5, P < 0.01). The study provides the first detailed analysis of the relatively late onset and short duration of strong opioid treatment in patients with cancer before death in a representative UK cohort. This pattern of prescribing does not match epidemiological data which point to earlier onset of pain. Although persistent undertreatment of cancer pain is well documented, this study suggests that strategies for earlier pain assessment and initiation of strong opioid treatment in community-based patients with cancer could help to improve pain outcomes.