Why did Mao inflict the horrors of the Cultural Revolution on China?

He wanted to keep his cred against rival up-and-comers.

If a country has a light-headed, erratic, conceited, garrulous, crafty semi-loon as chosen leader, they must expect interesting times.