A United States judge has rejected a Department of Justice request to temporarily halt California from counting mail ballots from a list of 20,000 alleged noncitizens just six days before the country’s midterm elections.

US District Judge David Carter’s ruling on Wednesday came as US President Donald Trump expands efforts to search for alleged noncitizen voters, with subpoenas issued in at least 13 states.

“Never before in American history has the federal government, on the eve of an election, sought to coerce a state and local government to abandon their own Constitutional elections procedures and bend to the will of the Executive Branch,” Carter wrote on Wednesday.

The judge, a 1998 appointee of Democratic President Bill Clinton, said the Justice Department’s last-minute request to exclude more than 20,000 ballots ran afoul of “longstanding constitutional principles mandating that states have control over their elections”.

The Justice Department swiftly appealed the decision to the Ninth US Circuit Court of Appeals and did not immediately respond to requests for comment by the Reuters news agency.

The Justice Department filed the lawsuit on October 15, a rare intervention in the voting process weeks before Tuesday’s election, when voters will choose which party controls Congress as well as their state and local representatives.

The decision marks the latest setback in Trump’s effort to expand the federal government’s role in elections ahead of the legislative midterms, as his Republican Party fights to retain control of both houses of Congress.

The Trump administration issued more than 100 subpoenas demanding records on tens of thousands of voters in at least 13 states, both Republican- and Democratic-leaning.

Most of the subpoenas Reuters documented were issued to state and local election officials in the past three months, though some date to the start of the year. Many were previously unreported, revealing a federal effort to identify potential noncitizen voters that is broader than publicly known.

It is illegal for noncitizens to vote in US federal elections. Legal experts say the records could be used to contest close midterm results, despite scant evidence that noncitizen voting is widespread.

Also on Wednesday, Democracy Docket, a US media platform that tracks election litigation, reported that the Department of Justice list of alleged noncitizen voters “appears to have been created using a deeply flawed process” that could disenfranchise US citizen voters beyond California.

In a court filing, a government whistleblower revealed that voters were being scrutinised nationwide, not just in California, as part of the federal government’s “Unlawful Voter Initiative” investigation, the group reported. The whistleblower warned that “the current process is knowingly sweeping known US citizens in as potentially ‘unlawful voters'”.

California has said the state’s safeguards against noncitizen voting are robust. It said the Justice Department’s assertions relied on “suspect data” and that complying with its demands would risk disenfranchising eligible voters. Democracy Docket reported that in Los Angeles County, one in four voters on the “noncitizen” list were born in the US.