A United States judge has ruled that the Justice Department unlawfully sought state voter rolls to cross-check against a federal immigration database and identify voters it considered ineligible.

US District Judge Sparkle Sooknanan’s decision marks another setback for President Donald Trump’s efforts to expand federal oversight of elections ahead of the midterms, as Republicans seek to retain control of Congress.

In a 78-page ruling siding with the watchdog group Common Cause, Sooknanan stated that the administration had understated the risk of wrongly removing eligible citizens from voter rolls due to inaccuracies within the Systematic Alien Verification for Entitlements database.

The challenged policy represents the federal government’s attempt to decide who gets to vote, Sooknanan wrote, adding that Congress had left that authority to the states. The Justice Department indicated it was reviewing the ruling.

Ming Cheung, a senior staff attorney with the American Civil Liberties Union’s Voting Rights Project, said the ruling made clear that the administration could not use the federal government to disrupt elections or put voters’ rights at risk. Cheung noted that the administration has repeatedly taken extreme measures to interfere with how elections are run, undermine confidence in results, and create confusion.

Last year, Trump directed the department to crack down on voting by ineligible people, despite research showing voter fraud is rare. The department subsequently demanded unredacted voter rolls from nearly all 50 states, including sensitive personal information.

Eighteen states, mostly Republican-led, complied with the request. The department then cross-checked their data against the federal database and flagged individuals identified as noncitizens for potential removal from voter rolls.

Sooknanan pointed out that the database could contain outdated citizenship records, which could potentially misidentify naturalized US citizens. The department has also sued 30 states and Washington, DC, over their refusal to provide voter data, with 26 of those cases having been dismissed.

In a separate case in June, Sooknanan blocked the expanded use of the database for voter roll checks, an order that the US Supreme Court temporarily paused. Federal law bars systematic voter roll purges in the 90 days before a federal election, which could limit the practical effect of the latest ruling before the upcoming vote.