At around 1 a.m. on Thursday morning, South African police entered a Cape Town neighborhood notorious for gang violence. It was one of several nationwide raids, in response to last month's attacks on foreigners in the country, officials said in a statement.

Accompanied by the military, police stalked between apartment buildings, arresting 39 people for crimes ranging from robbery to drug possession, according to a police statement. National police authorities said about 4,000 people were arrested as part of the sweep, including 1,650 foreigners living in the country illegally.

Authorities also deported undocumented immigrants arrested during the raids.

The sweeping raids began at the end of April in response to the violence targeting foreign migrants, in which seven people were killed. South African officials condemned the violence. The government is trying to address issues at the root of tension between immigrants and locals, according to a government statement.

Tackling crime has become one of the most visible responses, with police around the country reporting arrests as part of nationwide raids, known as Operation Fiela -- meaning to sweep clean in the local Sotho language. Despite criticism from rights groups, police have hailed the raids as a success.

South Africa's image has been tarnished by the attacks, especially among its African neighbors.

On a two-day state visit to the Mozambique, South African President Jacob Zuma apologized to the Mozambican people because a Mozambican national was killed in the attacks.

"It is important that I apologize in the name of a small minority of South Africans who carried out these atrocious acts," Zuma said at a banquet held by Mozambican President Filipe Nyusi, in the Mozambican capital Maputo on Wednesday evening.