In an extraordinary ruling, a State Supreme Court judge has ordered the developers of a nearly completed 668-foot condo tower on the Upper West Side to remove as many as 20 or more floors from the top of the building.

The decision is a major victory for community groups who opposed the project on the grounds that the developers used a zoning loophole to create the tallest building on the West Side north of 61st Street. A lawyer representing the project said the developers would appeal the decision.

Justice W. Franc Perry ordered on Thursday that the Department of Buildings revoke the building permit for the tower at 200 Amsterdam Avenue near West 69th Street and remove all floors that exceed the zoning limit. Exactly how many floors might need to be deconstructed has yet to be determined, but under one interpretation of the law, the building might have to remove 20 floors or more from the 52-story tower to conform to the regulation.

“We’re elated,” said Olive Freud, the president of the Committee for Environmentally Sound Development, one of the community groups that brought the suit.