In a sudden reversal, the de Blasio administration on Thursday embraced scrapping a much-ridiculed anti-dancing law that prohibits boogying in city bars and restaurants without a license.

While administration officials insist their position on the issue hadn’t changed, city lawyers argued in court as recently as June 2015 that “there is no protected First Amendment right of expression to engage in recreational dancing” under the cabaret law.

That opinion came in response to a lawsuit from Brooklyn bar owner Andrew Muchmore challenging the legality of the infamous law — which detractors said was originally intended to discourage interracial mingling at clubs in the 1920s.

“I don’t think it would be completely accurate to say they supported this all along. I filed the lawsuit in 2014 and they opposed it,” Muchmore told The Post. “It’s only now that they came around — and I’m happy they came around.”

The law imposes such onerous and expensive requirements on club and restaurant owners who want to get a license allowing patrons to dance that fewer than 100 of the city’s more than 22,000 establishments even bothered to comply, critics said.

But that left unlicensed venues open to haphazard enforcement by the city in recent decades — most notably in the 90s under the Giuliani administration, which cracked down on establishments in response to complaints about rowdy behavior.

At a City Council hearing on Thursday, city officials testified in favor of a repeal that keeps security measures of the law intact — clearing the way for the law to be nixed.

Bill sponsor Rafael Espinal (D-Brooklyn) called the support “very encouraging and a victory for the cause.”