Optimal glycaemic control has well-established health benefits in patients with diabetes mellitus (DM). It is uncertain whether optimal glycaemic control can benefit liver-related outcomes. To examine the association of optimal glycaemic control with hepatocellular carcinoma (HCC) and liver-related mortality. In a population-based cohort, we identified patients with newly diagnosed DM between 2001 and 2016 in Hong Kong. Optimal glycaemic control was defined as mean haemoglobin A1c (HbA1c) <7% during the 3-year lead-in period after DM diagnosis. By applying propensity score matching to balance covariates, we analysed glycaemic control via competing risk models with outcomes of interest being HCC and liver-related mortality. We identified 146,430 patients (52.2% males, mean age 61.4 ± 11.8 years). During a median follow-up duration of 7.0 years, 1099 (0.8%) and 978 (0.7%) patients developed HCC and liver-related deaths. Optimal glycaemic control, when compared to suboptimal glycaemic control, was associated with reduced risk of HCC (subdistribution hazard ratio [SHR] 0.70, 95% CI 0.61-0.79). The risk of HCC increased with incremental HbA1c increases beyond >7% (SHR 1.29-1.71). Significant associations with HCC were also found irrespective of age (SHR 0.54-0.80), sex (SHR 0.68-0.69), BMI <25 or ≥25 kg/m2 (SHR 0.63-0.75), smoking (SHR 0.61-0.72), hepatic steatosis (SHR 0.67-0.68) and aspirin/statin/metformin use (SHR 0.67-0.75). A lower risk of liver-related mortality in relation to optimal glycaemic control was also observed (SHR 0.70, 95% CI 0.61-0.80). Glycaemic control is an independent risk factor for HCC and liver-related mortality, and should be incorporated into oncoprotective strategies in the general DM population.