President Cyril Ramaphosa said replacing them will "save lives and restore dignity" to children.
When five-year-old Lumka Mkhethwa went missing at school in March, it was feared she had been abducted.
The next day her body was found. She had drowned after falling into a pit toilet in the grounds of Luna Primary in Bizana, South Africa.
The death of little Lumka caused an outcry. Now the government has announced that it will get ride of "hole-in-the-ground" toilets at more than 4500 state schools within two years.
“This is an initiative that will save lives and restore dignity to tens of thousands of our nation’s children, as our constitution demands,” said President Cyril Ramaphosa yesterday.
"It will spare generations of young South Africans the indignity, the discomfort and the danger of using pit latrines and other unsafe facilities in our schools.
"Schools should be places where children have fun, get educated, where they are safe." He said schools should be the "heartbeat of wholesome communities".
President Ramaphosa said the death of Lumka in rural Eastern Cape province had forced the government to act and ensure that decent sanitation is provided at all schools. Eastern Cape has more than 1500 schools with pit latrines and 61 with no toilets at all.
Many pit latrines are simply holes in the ground with poorly-constructed platforms that are left uncovered.
After her death, Lumka's father Vuyani Mkhethwa said: 'We do not understand how this happened. We were under the impression that children are escorted to the toilet at that age.”
In 2014, Michael Komape, who was also five, died after falling into a school toilet in South Africa's Limpopo province.
President Ramaphosa's comments came at the launch of the Sanitation Appropriate for Education Initiative in Pretoria. He said the initiative will see the government working with the private sector to eradicate pit toilets.
Lack of good toilet and sanitation facilities are one reason for girls in particular not going to school. More than 50% of primary schools in developing countries lack adequate water facilities and two-thirds have no adequate sanitation, according to UNICEF.
More than one in three girls in South Asia miss school during their periods every month, a report by WaterAid and UNICEF revealed in May. Major factors include inadequate toilet facilities that lack water, privacy or disposal options.