Protesters carrying banners and South African flags confront police during an anti-undocumented migration demonstration in Parklands, Cape Town. [AFP]

Several thousand African migrants have fled South Africa in recent weeks amid fears of escalating xenophobic violence.

One undocumented Malawian man told the BBC he was relieved to be returning home, but devastated to leave his four young children behind.

President Cyril Ramaphosa has urged protesters to remain peaceful while acknowledging the need for immigration reform.

In his weekly newsletter, Ramaphosa reminded South Africans that millions of foreign nationals live in the country legally.

"They work, study, raise families, invest in our economy and contribute positively to our society. They too are entitled to the protection of our laws and our Constitution," he wrote.

He stressed that while citizens have the right to protest, it does not extend to threats, intimidation, vandalism, or violence.

Official figures estimate that more than three million documented foreign nationals live in South Africa.

Ahead of an enforcement deadline, thousands of migrants have spent weeks in temporary camps awaiting processing after fleeing their communities out of fear for their safety.

Authorities have since dismantled transit camps in Durban, one of the cities that has witnessed some of the largest anti-immigration protests, as they move to clear the sites.

Many migrants said they came to South Africa in search of work to support their families. Nelson Mbewe, who travelled from Malawi, said they had been told to leave because they lacked the required documents. He also said they were exhausted by being called the xenophobic slur "makwerekwere."

"It's their country, so what can we do? That's why we have accepted that we just have to go back home," Mbewe said.

Another migrant, Hassan Phiri, who is still awaiting processing, appealed for unity across the continent.

"We are all one. Africa can't be Africa without South Africa, without Malawi, without anywhere," he said.

Authorities have warned protest organizers to prevent violence, saying security agencies are prepared for any disruption.

Police have also banned the carrying of traditional weapons during marches, a move that has become a potential flashpoint because many ethnic Zulu protesters customarily carry shields, whips, and sticks.

The government says more than 12,000 immigrants have been deported or repatriated since the wave of protests began earlier this year.

Ghana, Malawi, Mozambique, Nigeria, and Zimbabwe have organized air and bus repatriation efforts, with about 3,500 foreign nationals volunteering to return home.

South African officials say more than 500 repatriated Nigerians lacked proper documentation, a claim Nigeria disputes.

Xenophobic violence has a long history in South Africa.

The African Centre for Migration and Society's Xenowatch tracker has recorded two deaths linked to anti-foreigner attacks this year. It also notes that more than 60 people were killed during the country's 2008 anti-immigrant riots.