Updated

A federal judge in California on Friday ordered the government to release immigrant children from family detention centers “without unnecessary delay,” and with their mothers when possible, according to court papers.

California U.S. District Judge Dolly Gee refused the government’s request to reconsider her ruling in late July that children held in family detention centers after cross the US-Mexico border illegally must be released quickly.

Gee called the government’s latest arguments “repackaged and reheated.’ She found Homeland Security in beach of a longstanding legal agreement stipulating that immigrant children cannot be held in unlicensed facilities, and gave agency officials until October 23 to comply.

Homeland Security lawyers asked the judge to reconsider her ruling, arguing that the agency was already doing its best to move families through detention quickly and that the facilities had been converted into short-term processing centers.

This is the second time Gee has ruled that detaining children violates parts of a 1997 settlement from an earlier case. The settlement requires minors to be placed with a relative or in appropriate non-secure custody within five days. If there is a large influx of minors, times may be longer, but children still must be released as expeditiously as possible, under the terms of the law.

Gee has countered that immigration officials “routinely failed to process as expeditiously as possible to place accompanied minors, and in some instances, may still be unnecessarily dragging their feet now.”

Peter Schey, executive director of the Center for Human Rights and Constitutional Law, said that the court's order "will protect refugee children and their mothers from lengthy and entirely senseless detention."

The government poured millions of dollars into two large detention centers in Texas after thousands of immigrant families, mostly mothers with children from Central America, crossed the Rio Grande into the U.S. last summer. Many have petitioned asylum after fleeing gang and domestic violence in their home countries.

Two centers outside San Antonio recently held more than 1,300 women and children combined. A third, smaller facility in Pennsylvania, held about 70 people. All three are overseen by Immigration and Customs Enforcement but managed by private prison operators.

Between September 2013 and October 2014, some 68,000 family members — mostly mothers with children in tow — were caught at the border, according to U.S. Customs and Border Protection. Between last October and June of this year, less than 27,000 have been apprehended, a drop authorities say is a result of better enforcement in both the U.S. and Mexico.

In her order Friday, Gee challenged Homeland Security's claim that drastically limiting or ending its family detention policy could spark another surge in illegal border crossings, calling this "speculative at best" and "fear-mongering."

The Associated Press contributed to this report.