Israel can continue to deport illegal migrants to third countries, but cannot continue its policy of indefinite detention, its Supreme Court ruled.

The illegal migrants, mostly from Sudan and Eritrea in Africa, can now only be held in jail or a detention Centre for no more than 60 days, the court ruled Monday.

The judges said it must be determined the countries to which the migrants are deported, generally Rwanda and Uganda, are safe for the African nationals. Rwanda and Uganda will not accept involuntary deportees.

Israel is one of only a few Western countries who deport asylum seekers to third countries. Reports have emerged of migrants being mistreated after living in the third countries.

Asylum seekers who refuse to be deported to their country of origin or a third country can be jailed for up to two months while the state tries to persuade them to change their minds, the court said, but after that they must be freed. The state can bar them from living in select cities that already have large populations of asylum seekers, the court also said.

All five judges approved the decision.

The lawsuit was filed on behalf of two Eritreans and joined by several Israeli human rights groups.

More than 46,400 Africans living in Israel consider themselves asylum seekers, The Times of Israel reported, citing the African Refugee Development Centre.

Israel built a fence along its border with Egypt to keep out infiltrators.

Mark Hetfield, president and CEO of HIAS, the global Jewish nonprofit that protects refugees, called the decision a “partial victory for asylum seekers and their advocates in Israel.”

Hetfield said in a statement that he “welcomes the Court’s decision to end indefinite detention as a means of pressuring asylum seekers to leave to third countries.”

“At the same time, HIAS remains firmly opposed to Israel’s secretive system of deportations to unnamed countries that do not guarantee the protection of the individuals sent there,” he said. “Consistent with Jewish values and human rights, asylum seekers living in Israel must never be sent back into harm’s way.”