Dozens of migrant families arrive at a bus station in McAllen, Texas following release from Customs and Border Protection on June 23, 2018. Photo: Spencer Platt/Getty Images

After the Supreme Court handed President Trump a huge victory by upholding his travel ban on Tuesday, the day ended with him losing a separate battle with the judiciary. A federal judge in San Diego ordered the Trump administration to stop separating migrant families at the border, and to return the roughly 2,000 separated children still in the government’s care to their families within the next 30 days. The ruling said the “situation has reached a crisis level.”

Judge Dana Sabraw of the United States District Court for the Southern District of California granted the nationwide preliminary injunction in a class action brought by the American Civil Liberties Union on behalf of parents separated from their children under Trump’s “zero tolerance” policy. In addition to ordering that all families be reunited in 30 days, he said children under 5 must be returned to their parents in 14 days, and all parents must be allowed to speak to their children by phone within 10 days. Sabraw said parents can only be detained or deported without their children if the minors are in danger or their parents have consented.

The Justice Department had urged the judge not to issue the injunction, arguing that the executive order Trump issued last week addressed the ACLU’s concerns, and the government just needed time to reunite families. The executive order said families would be detained together as the parents await prosecution, but on Monday the administration said it would temporarily stop handing over all migrant parents for prosecution due to lack of resources.

Immigration attorneys say parents trying to find their children have been met with bureaucratic chaos, and at a hearing last week a Justice Department attorney admitted that there is no formal procedure for reuniting families, and that sometimes the Office of Refugee Resettlement, which is responsible for the children, doesn’t know that their parent has been released.

Sabraw blamed the Trump administration for “a chaotic circumstance of the government’s own making,” and said “placing the burden on the parents to find and request reunification with their children under the circumstances presented here is backwards.”

He added, “The unfortunate reality is that under the present system, migrant children are not accounted for with the same efficiency and accuracy as property.”

Lee Gelernt, the lead lawyer in the case for the ACLU, praised the judge’s decision.

“This is an enormous win and will mean that this humanitarian crisis is coming to an end,” Gelernt said. “We hope the Trump administration will not think about appealing when the lives of these little children are at stake.”

The Trump administration has yet to respond, but it could appeal the decision to the U.S. Court of Appeals for the Ninth Circuit.

The ruling came hours after 17 states filed a federal lawsuit over Trump’s family separation policy, alleging that the administration has violated the constitutional rights of the separated parents and children, and was motivated by “animus” toward Latinos. The government has yet to comment on that suit as well.