President Trump abruptly fired FBI Director James Comey late Tuesday for a startling reason — ending the investigation into Hillary Clinton’s email server.

“The FBI’s reputation and credibility have suffered substantial damage, and it has affected the entire Department of Justice,’’ Deputy Attorney General Rod Rosenstein wrote in a memo.

“I cannot defend the director’s handling of the conclusion of the investigation of Secretary Clinton’s emails, and I do not understand his refusal to accept the nearly universal judgment that he was mistaken. Almost everyone agrees that the director made serious mistakes; it is one of the few issues that unites people of diverse perspectives.’’

Trump sent his longtime personal security guard, Keith Schiller, to hand-deliver notification of Comey’s termination to FBI headquarters. Comey was in LA giving a speech to FBI agents at the time.

The president issued a public statement saying that the FBI needed fresh leadership.

“The FBI is one of our nation’s most cherished and respected institutions and today will mark a new beginning for our crown jewel of law enforcement,” the president said.

Trump said he acted on the recommendations of both Rosenstein and Attorney General Jeff Sessions, who also concluded Comey could no longer serve effectively.

The Rosenstein memo quoted Donald Ayer, who served as deputy attorney general under President George H.W. Bush, as criticizing Comey.

Ayer, however, later told BuzzFeed News that the reasons for firing Comey were a “sham” and that Rosenstein should have realized that Ayer’s assessment of Comey’s mistakes was now being used for a “very different reason.”

The move came on the same day the FBI issued a “supplement” to Comey’s testimony before Congress last week, in which he overstated the number of emails Clinton aide Huma Abedin had sent to husband Anthony Weiner’s laptop — which Comey cited as a reason for reopening the Clinton probe only a week before the election.

Comey’s ouster came as the FBI continues an investigation into possible ties between the Trump campaign and administration to Russia.

Democrats criticized the timing of the firing and called for a special prosecutor to investigate the administration’s alleged ties to Russia.

“Every American will rightly suspect that the decision to fire Director Comey was part of a cover-up,” Sen. Charles Schumer said Tuesday night, adding that Trump called him to inform him of the decision.

“I told the president, ‘Mr. President, with all due respect, you are making a big mistake.’ ”

Trump later blasted back, tweeting, “Cryin’ Chuck Schumer stated recently, “I do not have confidence in him (James Comey) any longer.” Then acts so indignant.”

Clinton’s campaign manager, Robby Mook, who had blamed Comey, in part, for her loss to Trump, tweeted, “I was as disappointed and frustrated as anyone at how the email investigation was handled. But this terrifies me.”

The move was called “Nixonian” by Democratic Sen. Bob Casey of Pennsylvania.

Meanwhile, GOP Sen. John McCain said Comey’s firing “confirms the need and the urgency” for a congressional committee on Trump’s Russia ties. Rep. Justin Amash (R-Mich.) tweeted that he was “reviewing legislation to establish an independent commission on Russia.”

The New York Times reported that senior White House officials, along with Sessions, had been tasked with finding a reason to fire Comey since last week. It was not clear why.

The Wall Street Journal (paywall) reported that the White House was frustrated that Comey refused to knock down questions of collusion with Russia and that the administration wanted him to “say those three little words: ‘There’s no ties.’ ”

FBI workers were stunned by the news. “I found out when my wife texted me, and even then I didn’t believe it,” one told The Post.

A source said even the Senate Judiciary Committee, which oversees the FBI, was not given a heads-up.

In the letter terminating Comey, Trump expressed his appreciation to the FBI boss for telling him he was not the target of the “Russia” investigation.

“While I greatly appreciate you informing me, on three separate occasions, that I am not under investigation, I nevertheless concur with the Department of Justice that you are not able to effectively lead the bureau,” Trump wrote.

He concluded by wishing the Yonkers native “the best of luck in your future endeavors.”

After news of the firing sent shock waves throughout the political establishment, Trump aides scrambled to defend the action with appearances on cable news.

Rudy Giuliani, who was once considered a candidate for attorney general, told The Post on Tuesday that Comey was “excellent.”

“When I was US attorney, Jim was one of my assistant attorneys,” Giuliani said Tuesday, adding that Comey made “a couple mistakes” and had to be fired.

Former FBI assistant director Ron Hosko told The Post that Comey “became a victim of politics” and offended a number of Justice Department officials who thought he was “usurping the role of a prosecutor.”

With AP