A former NYPD cop once assigned to guard City Hall and the mayor’s residence at Gracie Mansion was arrested for allegedly beating a police officer with a pole at the Jan. 6 Capitol riot, prosecutors and police sources said Tuesday.

Thomas Webster, 54, a 20-year NYPD veteran who also served in the Marine Corps, turned himself into the FBI Monday in Hudson Valley and made an initial court appearance in White Plains federal court Tuesday.

At the hearing, Assistant U.S. Attorney Ben Gianforti said Webster was captured on video beating a Metropolitan police officer with a flag pole during the uprising at the Capitol building that day.

Webster, who was seething in rage, was calling the police officer a "f—king piece of s—t" and a "Commie motherf—ker," the prosecutor said.

DEFENSE SECRETARY SAYS NATIONAL GUARD TROOPS TO LEAVE BY MID-MARCH, BUT LONG-TERM SECURITY CONCERNS LINGER

Judge Andrew Krause said he was shocked by the evidence against Webster.

"The conduct on the video does shock the conscience. What we see is a person who flat out attacks a law enforcement officer," Krause said.

Krause ordered the former cop, who retired from the force in 2011, to remain behind bars pending his trial, saying he believed Webster would pose a threat to the community if he was released. 

His defense attorney argued the veteran voluntarily turned over his weapons before his arrest — and said he traveled to the Capitol that day to participate in a protest that was promoted by then-President Donald Trump.

"He went there to participate in a protest, judge. He went there as an American citizen to protest," Webster’s attorney said.

Five people, including a Capitol police officer, died after throngs of Trump supporters stormed the U.S. Capitol to overturn the 2020 presidential election.

During his years in the department, Webster was assigned to a uniformed detail that protected both City Hall and Gracie Mansion, a police source said.

CLICK HERE TO GET THE FOX NEWS APP   

Webster faces a slew of charges for his role in the riot, including assaulting, resisting or impeding officers with a deadly or dangerous weapon, according to court papers.

To read more from The New York Post, click here.