Marchers in San Diego protest against the separation of families detained at the US-Mexico border.
Photograph: David Maung/EPA

A federal judge has ordered that US immigration agents can no longer separate parents and children caught crossing the border from Mexico illegally and must work to reunite those families that have been split up within 30 days.

Dana Sabraw, a district court judge in San Diego, granted the American Civil Liberties Union (ACLU) a preliminary injunction on Tuesday, which set a hard deadline in a process that until now had yielded uncertainty about when children might again see their parents.

Children younger than five must be reunited with their families within 14 days, the court ruled.

“Tears will be flowing in detention centres across the country when the families learn they will be reunited,” said Lee Gelernt, an ACLU lawyer, after the ruling.

It is not clear how border authorities will meet the deadline, however. The health and human services secretary, Alex Azar, told Congress on Tuesday that his department still had custody of 2,047 children separated from their parents at the border. That is only six fewer than the number in HHS custody last Wednesday. Democratic senators said that was not nearly enough progress.

Sabraw, an appointee of former president George W Bush, issued a nationwide injunction on future family separations, except where the parent is deemed unfit or does not want to be with the child. His ruling requires that the government provide phone contact between parents and their children within 10 days.

Following national and international outcry, Donald Trump signed an executive order last week mandating that families entering the US illegally would no longer be separated under his administration’s “zero-tolerance” immigration policy.

Before the preliminary injunction ruling, the government urged Sabraw not to rule that it must stop separating families and quickly reunite them, saying Trump’s executive order last week largely addressed those goals.

But the judge disagreed. “The facts set forth before the court portray reactive governance responses to address a chaotic circumstance of the government’s own making,” Sabraw wrote. “They belie measured and ordered governance, which is central to the concept of due process enshrined in our constitution.”

Tuesday’s ruling – which came after the supreme court upheld Trump’s travel ban targeting five Muslim-majority countries – could open up a legal battle with the justice department, which has blamed elected politicians in Congress for the mess.

The DoJ said the ruling “makes it even more imperative” for Congress to pass immigration legislation that would enable the government “to simultaneously enforce the law and keep families together”.

“Without this action by Congress, lawlessness at the border will continue,” the department said.

Congress is braced for a vote later on Wednesday on an immigration bill that is designed as a compromise between moderate and conservative factions of the congressional Republicans. It is on track for defeat.

The bill would provide $25bn in US taxpayers’ money for a wall on the Mexican border, even though Trump pledged repeatedly during the 2016 election campaign that he would force Mexico to pay for it.

It would also limit legal immigration; provide a pathway to citizenship for eligible young undocumented immigrants, known as Dreamers; and officially ban family separations at the border. But Democrats and hardline conservative Republicans are refusing to support it.

In the face of defeat, some Republicans were considering a plan B: passing narrower legislation by the end of the week curbing the Trump administration’s contentious separating of migrant families.

Simon Sandoval-Moshenberg, of the Legal Aid Justice Centre in Virginia, said attorneys had spoken to about 200 immigrants at the Port Isabel detention facility near Los Fresnos, Texas, since last week, and only a few knew where their children were being held.

“The US government never had any plan to reunite these families that were separated,” he said, and it was now “scrambling to undo this terrible thing that they have done”.

Seventeen states sue Trump administration over family separations

Read more

On Tuesday, 17 states including New York and California sued the Trump administration to try to force it to reunite children and parents. The states, all of which have Democratic attorneys general, joined Washington DC in filing the lawsuit in a federal court in Seattle, arguing that they were being forced to shoulder increased child welfare, education and social services costs.

In a speech before the conservative Criminal Justice Legal Foundation in Los Angeles, the US attorney general, Jeff Sessions, defended the administration for taking a hardline stand on illegal immigration and said voters had elected Trump to do just that.

“This is the Trump era,” he said. “We are enforcing our laws again. We know whose side we are on … and we’re on the side of police and we’re on the side of the public safety of the American people.”

Many people outraged by the family separations have staged protests in recent days in states including Florida and Texas. In Los Angeles, police arrested 25 demonstrators at a rally on Tuesday before Sessions gave his address.

Outside Sessions’ office, protesters carried signs reading “Free the children!” and “Stop caging families”. Clergy members who blocked the street by forming a human chain were handcuffed by police and led away.