JERUSALEM — Israel on Monday canceled the planned expulsions of tens of thousands of African migrants, saying it reached a deal with the United Nations to resettle half of them in Western nations and allow the rest to remain in the country.

The surprise announcement offered a solution to an issue that has divided Israel for the past decade and scrapped a plan that had been widely maligned at home and abroad, even by some of Israel’s closest supporters.

“It’s a good agreement,” Prime Minister Benjamin Netanyahu told reporters. “It enables us to solve this problem in a way that serves, protects the interests of the state of Israel and gives a solution to the residents of southern Tel Aviv and other neighborhoods, and also for the people who came into Israel.”

Israel is home to roughly 35,000 African migrants, most of them from Eritrea, which has one of the world’s worst human rights records, or war-torn Sudan. The migrants say they are asylum-seekers fleeing danger and persecution, while Israeli leaders have claimed they are merely job seekers.

The Africans started arriving in 2005, after neighboring Egypt violently quashed a refugee demonstration and word spread of safety and job opportunities in Israel.

Tens of thousands crossed the porous desert border before Israel completed a barrier in 2012 that stopped the influx. But Israel has struggled with what to do with those already in the country, alternating between plans to deport them and offering them menial jobs in hotels and local municipalities.

Thousands of the migrants concentrated in poor neighborhoods in south Tel Aviv, an area that has become known as “Little Africa.” Their presence has sparked tensions with working-class Jewish residents, who have complained of rising crime and pressed the government to take action.

But the migrants also found wide pockets of support, with many Israelis arguing that the country, founded in the wake of the Holocaust, had a special responsibility to help those in need. Thousands of Africans and their Israeli supporters held a demonstration in February claiming that the deportation plan amounted to racism.

Groups of Israeli doctors, academics, poets, Holocaust survivors, rabbis and pilots had also objected to the planned expulsion.

Critics at home and in the Jewish American community had called the government’s deportation plans unethical and a stain on Israel’s image as a refuge for Jewish migrants.

Before Monday’s announcement, the government had remained steadfast, bristling at what it considered cynical comparisons to the plight of Jewish refugees during World War II.

Over the years, Israel had threatened migrants with prison, placed them in a now-shuttered desert detention camp and tried to persuade them to leave by offering them money and a one-way ticket to Africa. After those options failed, it announced plans to begin deporting them to an unnamed African country — believed to be Rwanda or Uganda — on April 1.

Netanyahu said the plan was scrapped after it became clear that the “third country,” which he did not identify, could not handle the influx.

“From the moment in the past few weeks that it became clear that the third country as an option doesn’t exist, we basically entered a trap where all of them would remain,” he said. He described Monday’s compromise as the best available option.

Migrants who had earlier agreed to return to Africa had reported abuses and broken promises after arriving in Rwanda. In some cases, their travel papers were confiscated or they were quickly sent to Uganda. Both Rwanda and Uganda had denied having any agreement with Israel.

Meir Ben-Shabbat, Netanyahu’s national security adviser, said the plan would be carried out in three phases over five years. He said the deal would apply only to migrants currently in Israel.