Background: The mechanisms linking the human immunodeficiency virus (HIV) to cancer are not fully understood. We analysed associations between immunodeficiency and the incidence of various infection-related and infection-unrelated cancers in a large cohort of people living with HIV (PLWH).Methods: We used data from the South African HIV Cancer Match (SAM) study which is the result of probabilistic record linkage between HIV laboratory measurements provided by the National Health Laboratory Services and cancer records from the National Cancer Registry in South Africa. We classified cancers based on type and related infections. For each of these cancer groups, we calculated crude incidence rates and evaluated associations between time-updated CD4 cell count and cancer incidence rates using Cox proportional hazards models. We reported the associations using adjusted hazard ratios (aHR) over a grid of CD4 values for a reference value of 200 cells/µl, and by estimating the aHR per decrease of 100 CD4 cells/µl after assuming a linear relationship between the log hazard and CD4 cell counts. Results: We analysed 3,695,723 PLWH, of which 16,274 developed cancer, for an overall crude cancer incidence rate of 169 per 100,000 person-years (py). The most common cancers were cervical cancer (4,151 cases in women, rate of 59 per 100,000 py), Kaposi Sarcoma (2,311 cases, rate of 24 per 100,000 py), and non-Hodgkin lymphoma (1,101 cases, rate of 11 per 100,000 py). There were 6,482 PLWH diagnosed with cancer not related to infection (rate of 67 per 100,000 py). The association between low CD4 cell count and higher rates of cancer was strongest in conjunctival cancer (aHR per decrease of 100 CD4 cells/µl: 1.47, 95% confidence interval [CI] 1.39-1.55), followed by Kaposi Sarcoma (aHR 1.23, 95% CI 1.20-1.26) and non-Hodgkin lymphoma (aHR 1.16, 95% CI 1.12-1.19). Among the infection-unrelated cancers, we found low CD4 cell count to be associated with higher rates of squamous cell carcinoma of the skin (aHR 1.06, 95% CI 1.02-1.11) and cancer of the oesophagus (aHR 1.06, 95 CI 1.00-1.11). We found no association between CD4 cell count and both breast and prostate cancer incidence.Conclusions: Low time-updated CD4 cell counts were associated with an increased risk of developing various infection-related cancers among PLWH. Reducing HIV-induced immunodeficiency may be a potent cancer prevention strategy among PLWH in South Africa, a region heavily burdened by cancers attributable to infections.