Little is known about the functional relationship of delaying second-line treatment initiation for human immunodeficiency virus–positive patients and mortality, given a patient’s immune status. We included 7,255 patients starting antiretroviral therapy during 2004–2017, from 9 South African cohorts, with virological failure and complete baseline data. We estimated the impact of switch time on the hazard of death using inverse probability of treatment weighting of marginal structural models. The nonlinear relationship between month of switch and the 5-year survival probability, stratified by CD4 count at failure, was estimated with targeted maximum likelihood estimation. We adjusted for measured time-varying confounding by CD4 count, viral load, and visit frequency. Five-year mortality was estimated to be 10.5% (95% CI: 2.2, 18.8) for immediate switch and to be 26.6% (95% CI: 20.9, 32.3) for no switch (51.1% if CD4 count was <100 cells/mm3). The hazard of death was estimated to be 0.37 (95% CI: 0.30, 0.46) times lower if everyone had been switched immediately compared with never. The shorter the delay in switching, the lower the hazard of death—delaying 30–59 days reduced the hazard by 0.53 (95% CI: 0.43, 0.65) times and 60–119 days by 0.58 (95% CI: 0.49, 0.69) times, compared with no switch. Early treatment switch is particularly important for patients with low CD4 counts at failure.