Health disorders, such as milk fever, displaced abomasum, or retained placenta, as well as poor reproductive performance, are known risk factors for culling in dairy cows. Clinical mastitis (CM) is one of the most influential culling risk factors. However the culling decision could be based either on the disease status or on the current milk yield, milk production being a significant confounder when modelling dairy cow culling risk. But milk yield (and somatic cell count) are time-varying confounders, which are also affected by prior CM and therefore lie on the causal pathway between the exposure of interest, CM, and the outcome, culling. Including these time-varying confounders could result in biased estimates. A marginal structural model (MSM) is a statistical technique allowing estimation of the causal effect of a time-varying exposure in the presence of time-varying covariates without conditioning on these covariates. The objective of this paper is to estimate the causal effect on culling of CM occurring between calving and 120 days in milk, using MSM to control for such time-varying confounders affected by previous exposure. A retrospective longitudinal study was conducted on data from dairy herds in the Province of Québec, Canada, by extracting health information events from the dairy herd health management software used by most Québec dairy producers and their veterinarians. The data were extracted for all lactations starting between January 1 and December 31, 2010. A total of 3,952 heifers and 8,724 cows from 261 herds met the inclusion criteria and were used in the analysis. The estimated CM causal hazard ratios were 1.96 [1.57--2.45] and 1.47 [1.28--1.69] for heifers and cows, respectively, and as long as causal assumptions hold. Our findings confirm that CM was a risk factor for culling, but with a reduced effect compared to previous studies, which did not properly control for the presence of time-dependent confounders such as milk yield and somatic cell count. Cows experienced a lower risk for CM, with milk production having more influence on culling risk in cows than heifers.