Safety of anti-tumour necrosis factor-α (TNFα) therapy in people with a history of cancer and with an immune-mediated disease is unknown. We aimed to assess the risk of recurrence of initial cancer or development of a new primary cancer after treatment with anti-TNFα therapy. In this Danish, population-based cohort study we recruited adults (≥18 years) with inflammatory bowel disease (IBD), rheumatoid arthritis, or psoriasis and a primary cancer diagnosed between Jan 1, 1999 and Dec 31, 2016. Patients were recruited from the prospectively recorded Danish National Patient Registry and the Danish Cancer Registry. Participants were matched 1:10 between the treatment group who received anti-TNFα therapy and the control group (no anti-TNFα therapy) and we excluded individuals with a cancer diagnosed before their first anti-TNFα treatment (or before matching date for controls), individuals diagnosed with IBD, rheumatoid arthritis, or psoriasis after anti-TNFα initiation (or respective match date for controls), and individuals who received anti-TNFα with fewer than five matched controls. Using adjusted Cox proportional hazards regression, we estimated the primary outcome of development of recurrent or new primary cancer in patients who received anti-TNFα therapy compared with patients who did not receive this therapy, matched by sex, immune-mediated disease type, cancer type, and time from initial cancer diagnosis to first anti-TNFα registration. Overall, 25 738 patients with immune-mediated disease and a history of cancer were identified. 434 patients who received anti-TNFα therapy after their initial cancer were matched to 4328 patients in the control group. During 18 752 person-years (median 5·6 years [IQR 2·8-7·9]) of follow up, 635 individuals developed recurrent or new primary cancer, 72 of whom had received anti-TNFα therapy and 563 of whom were in the control group. The median time between anti-TNFα treatment and recurrent or new primary cancer diagnosis was 2·8 years (IQR 1·7-5·4). The incidence of recurrent or new primary cancer development was 30·3 cases (95% CI 24·0-38·2) per 1000 person-years in the anti-TNFα treatment group and 34·4 cases (31·7-37·3) per 1000 person-years in the control group, yielding an adjusted hazard ratio of 0·82 (95% CI 0·61-1·11). Use of anti-TNFα therapy was not associated with recurrent or new primary cancer development in patients with previous cancer. Timing of anti-TNFα therapy after an initial cancer diagnosis did not influence recurrent or new primary cancer development. This observation might guide clinical decision making among providers treating immune-mediated diseases with anti-TNFα medications. None.