NEW YORK (AP) — The New York Democratic presidential primary must take place June 23 because canceling it would be unconstitutional and deprive withdrawn presidential candidates Bernie Sanders and Andrew Yang of proper representation at the Democratic convention, a judge ruled Tuesday.

U.S. District Judge Analisa Torres in Manhattan ruled after hearing arguments a day earlier as lawyers for Sanders and Yang argued that it was wrong to cancel the primary.

The judge said there was enough time before the primary occurs to figure out how to carry it out safely.

Torres wrote that “the removal of presidential contenders from the primary ballot not only deprived those candidates of the chance to garner votes for the Democratic Party’s nomination, but also deprived their pledged delegates of the opportunity to run for a position where they could influence the party platform, vote on party governance issues, pressure the eventual nominee on matters of personnel or policy, and react to unexpected developments at the Convention."

It also “deprived Democratic voters of the opportunity to elect delegates who could push their point of view in that forum," she said. “The loss of these First Amendment rights is a heavy hardship."

The Democratic members of the State’s Board of Elections voted last week to cancel the presidential primary even though New York still planned to hold its congressional and state-level primaries June 23.

They cited the danger to voters from the coronavirus as a reason to cancel the election since former Vice President Joe Biden has been endorsed by the major candidates he had faced.

The fact that the primary was going to occur on June 23 anyway because of other contested races, including a number of congressional primaries, led Torres to question on Monday why the primary wasn't canceled entirely if safety was such a concern.

Asked for reaction to the ruling, New York state Democratic party chair Jay Jacobs said: “We are reviewing it.”

Jacobs had called holding the primary “unnecessary” with the suspension of Sanders’ campaign and said reduced turnout could reduce the need for as many poll workers.

State board of elections spokesman John Conklin said: “No comment at this time. Our lawyers are reviewing the decision.”

Biden’s campaign declined to comment. The campaign has kept its distance from the situation, saying the campaign didn’t ask for the primary to be scrapped and not wanting to become embroiled in a new fight over nearly 300 delegates to the summer convention.

Biden became the presumptive nominee when Bernie Sanders suspended his campaign last month, but Sanders had made clear that he wanted to continue collecting delegates from remaining primaries, including in New York, to maximize his influence over the party platform and other decisions at the Democratic convention this summer.

Biden’s campaign did not want to be seen as stepping on Sanders’ efforts to do that in a state like New York, where the Vermont senator maintains a significant following.

Sanders’ allies celebrated the ruling.

“Credit to Andrew Yang and all the grassroots groups that have been carrying on the fight for democracy in New York,” said Larry Cohen, who chairs Our Revolution, the grassroots organization spun out of Sanders’ 2016 White House bid. “Vote by mail is the answer to the pandemic,” Cohen continued, “not canceling the presidential primary when more than 80% of democrats have other elections the same day.”

Sanders' representatives, in a statement forwarded by attorney Arthur Schwartz, who argued before Torres, called the decision “an extraordinary victory for the democratic process here in New York, a state much in need of something to cheer about."

Attorney Jeff Kurzon, representing Yang and Congressional candidate Jonathan Herzog, said he was thankful that the judge upheld the laws and protected the right to vote.

“We are fired up and up and ready to go vote on June 23!," he wrote in an email.

Associated Press Writer Bill Barrow reported from Atlanta and Marina Villeneuve reported from Albany.