President Trump’s decision Tuesday to fire FBI Director James Comey took Washington by surprise — but was likely inevitable. Comey had lost the trust of Democrats and Republicans alike.

The announcement came just hours after the FBI notified Congress that the director’s Senate testimony last week got key facts badly wrong about the Hillary Clinton e-mails that wound up on Anthony Weiner’s computer via his wife, Huma Abedin.

And that capped months of Comey actions that infuriated one party or the other. Many Democrats think his decision to “go public” on the Weiner e-mails cost Hillary Clinton the election.

Officially, Comey was fired for his July public statement recommending that no criminal charges be brought against Clinton or anyone else over her e-mail mess.

That, said Deputy Attorney General Rod Rosenstein in a letter to Trump, “usurp[ed] the authority” of then-Attorney General Loretta Lynch — though Lynch’s conflict of interest after her tarmac tête-à-tête with Bill Clinton had prompted Comey to do it in the first place.

He also blasted Comey for describing Clinton’s handling of her private server as “extremely careless.” Said Rosenstein: “We do not hold press conferences to release derogatory information about the subject of a declined criminal investigation.”

Which is why Attorney General Jeff Sessions concluded “a fresh start is needed at the leadership of the FBI.”

Comey explained his reasons for each move, and they seemed well-intended. But each decision sparked resentment and frustration — doing, per Rosenstein, “substantial damage” to the FBI’s credibility.

It’s a sad ending to the career of a dedicated public servant, but undoubtedly the right call.