A federal judge in California has ruled that the Obama administration’s detention of children and their mothers who were caught crossing the border illegally is a serious violation of a longstanding court settlement, and that the families should be released as quickly as possible.

In a decision late Friday roundly rejecting the administration’s arguments for holding the families, Judge Dolly M. Gee of Federal District Court for the Central District of California found that two detention centers in Texas that the administration opened last summer fail to meet minimum legal requirements of the 1997 settlement for facilities housing children.

Judge Gee also found that migrant children had been held in “widespread deplorable conditions” in Border Patrol stations after they were first caught, and she said the authorities had “wholly failed” to provide the “safe and sanitary” conditions required for children even in temporary cells.

The opinion was a significant legal blow to detention policies ordered by Homeland Security Secretary Jeh Johnson in response to an influx of children and parents, mostly from Central America, across the border in South Texas last summer. In her 25-page ruling, Judge Gee gave a withering critique of the administration’s positions, declaring them “unpersuasive” and “dubious” and saying officials had ignored “unambiguous” terms of the settlement.