A federal judge has ruled that the US Department of Justice's current system of detaining children with their mothers after they crossed the US-Mexico border violates an 18-year-old court settlement.
The decision on Friday by US District Judge Dolly Gee in California is a victory for the immigrant rights lawyers who brought the case, but its immediate implications for detainees were not immediately clear.
The ruling upholds a tentative decision Gee made in April, and comes a week after the two sides told her that they failed to reach a new settlement agreement as she had asked for.
The 1997 settlement at issue bars immigrant children from being held in unlicensed facilities. Gee found that settlement covered all children in the custody of federal immigration officials, even those being held with a parent.
Peter Schey, executive director of the Center for Human Rights and one of the attorneys who brought the suit, said federal officials "know they are in violation of the law".
"They are holding children in unsafe facilities, it is that simple," Schey told the AP news agency. "It's intolerable, it is in humane, and it needs to end, and end sooner rather than later."
Justice Department attorneys did not immediately reply to late-night messages seeking comment on the ruling.
The new lawsuit was brought on by new major detention centres for women and children in Texas that are overseen by the US government but are managed by private prison operators.
Together they have recently held more than 2,000 women and children between them after a surge of tens of thousands of immigrants from Central America, most of them mothers with children, many of whom claimed they were fleeing gang and domestic violence back home.
They are often held in overcrowded facilities in the US that lack basic necessities such as beds and are forced to sleep on the floor.
The Justice Department had argued it was necessary to modify the settlement and use detention to try to deter more immigrants from coming to the border after last year's surge and it was an important way to keep families together while their immigration cases were being reviewed, but the judge rejected that argument in Friday's decision.
Gee said the Department of Justice has 90 days to show cause why it should not change its policies in according with her ruling.
But since the tentative ruling in April, Immigration and Customs Enforcement has vowed to make the facilities more child-friendly and provide better oversight.