A federal judge gave the Trump administration just 14 days to reunite young children with their illegal immigrant parents and ordered a full halt to most future separations in a ruling late Tuesday.

The decision by U.S. District Judge Dana M. Sabraw is the latest blow to the administration’s zero-tolerance border policy, which has been reeling in recent weeks under attacks from Congress, activists and now federal judges.

Judge Sabraw certified a class action, ordered the government to set up regular calls between parents and children who are separated, said children under 5 years of age must be reunited within 14 days and said no parent can be deported unless his or her children are with them.

The ruling is a near-total victory for immigrant-rights activists and a major setback for the Trump administration, which just hours earlier had begged the courts to keep out of the fast-evolving dispute.

Judge Sabraw said the government can still prosecute border-jumpers and can deport illegal immigrants, but it cannot do so at the expense of illegal immigrant parents’ rights to remain connected to their children.

“When children are separated from their parents under these circumstances, the government has an affirmative obligation to track and promptly reunify these family members,” he wrote.

He appeared to be moved by stories of illegal immigrants who said they cried, felt severe stress and were otherwise distraught after arriving illegally in the U.S. and learning they would not be able to stay with their children while in jail or detention.

He also cited one news account that reported an illegal immigrant committed suicide after being separated from his wife and 3-year-old child.

Immigrant-rights activists cheered the ruling as a major blow to the administration.

“Tears will be flowing in detention centers across the country when the families learn they will be reunited,” said Lee Gelernt, deputy director of the American Civil Liberties Union’s immigrants rights project.

President Trump signed an executive order last week prohibiting family separations, though he directed the government to continue prosecuting border jumpers. This week the head of U.S. Customs and Border Protection said they’ve stopped those prosecution referrals. He blamed Mr. Trump’s order, saying it made it impossible, for now, to both prosecute migrants and hold them in immigration detention.

The government has, however, struggled to reunite the more than 2,000 children in its custody who were separated.

In briefs filed earlier Tuesday, the government was unable to give a timetable or even to explain what concrete plans it has.

Judge Sabraw said the government had essentially put the burden on the illegal immigrants to find their children. He said that was backward, and it should be up to the government to make the connections and reunifications.

The Trump administration had asked courts not to wade into the thorny issues, saying they would only gum things up.

“A hasty injunctive ruling by this court on issues of this level of complexity would be as likely to slow and complicate reunification efforts as to speed them,” the government lawyers said.