Saying ICE has failed to protect the health of 400 immigrants held at two crowded facilities in California, a federal judge in San Francisco ordered the agency Wednesday to immediately provide their names, records and health conditions so he can decide whom to release during the coronavirus pandemic.

Migrants held by U.S. Immigration and Customs Enforcement at Mesa Verde in Bakersfield and the Yuba County Jail in Marysville “are still sleeping in barracks-style dorms within arms’ reach of one another” long after COVID-19 was declared a national emergency, said U.S. District Judge Vince Chhabria.

The agency’s failure to provide a list of medically vulnerable detainees six weeks after Gov. Gavin Newsom’s statewide shutdown order “speaks volumes about where the safety of the people at these facilities falls on ICE’s list of priorities,” Chhabria said.

He told ICE to promptly provide information on all detainees at both facilities and said he would then decide who should be set free on bail while their cases are pending in immigration courts.

Chhabria said he would not free anyone who was shown to be dangerous to the public or seemed unlikely to appear for a court hearing. But he said ICE “has not come close to achieving social distancing for most detainees” because of the number of people it holds and the conditions at both centers.

“Time is of the essence,” the judge said.

Chhabria told lawyers that, with the help of federal magistrates, he will be releasing the detainees during the next two weeks, said attorney Bree Bernwanger of the Lawyers’ Committee for Civil Rights in San Francisco. The two facilities are “not safe for anyone,” she said.

Other federal judges have ordered ICE to release detainees at the two facilities and others in California who are vulnerable to the coronavirus because of their age or medical condition.

U.S. District Judge Terry Hatter of Los Angeles issued an order April 23 requiring the agency to reduce the population of its detention center in Adelanto (San Bernardino County) to a level that would allow remaining inmates to stay at least 6 feet apart at all times. He said ICE could take such actions by releasing some detainees, deporting some and transferring others to less-crowded facilities.

The American Civil Liberties Union represents detainees in the Adelanto case and in Chhabria’s court. The ACLU released a declaration to the court by Lawrence Mwuara, 27, a Kenyan citizen and longtime U.S. resident who is being held by ICE in Yuba County despite health infirmities that the ACLU said put him at a particular risk from the coronavirus.

“Being unable to protect myself from infection here, especially because of my medical conditions, makes me feel like nothing — like dirt,” Mwuara said.

Bob Egelko is a San Francisco Chronicle staff writer. Email: begelko@sfchronicle.com Twitter: @BobEgelko