Defense Secretary James Mattis, who strongly opposed President Trump’s abrupt decision to yank US troops from Syria, will leave office at the end of February, the commander-in-chief announced Thursday.

“General Jim Mattis will be retiring, with distinction, at the end of February, after having served my Administration as Secretary of Defense for the past two years. During Jim’s tenure, tremendous progress has been made, especially with respect to the purchase of new fighting equipment,” Trump wrote on Twitter.

“General Mattis was a great help to me in getting allies and other countries to pay their share of military obligations. A new Secretary of Defense will be named shortly. I greatly thank Jim for his service!”

But Mattis, a retired general, suggested in a scathing resignation letter that he could no longer work for Trump — detailing his belief in strong relations with traditional US allies, whom Trump has regularly scorned.

“One core belief I have always held is that our strength as a nation is inextricably linked to the strength our unique and comprehensive system of alliances and partnerships,” Mattis wrote to Trump in a letter read aloud by a reporter on CNN.

“While the US remains the indispensable nation in the free world, we cannot protect our interests or serve that role effectively without maintaining strong alliances and showing respect to those allies.”

Trump has slammed NATO and its members, picked fights with the leaders of the UK, France, Germany and Canada, and cozied up to longstanding rivals including Russia and China.

Mattis alluded to those countries in his letter to Trump.

“I believe we must be resolute and unambiguous in our approach to those countries whose strategic interests are increasingly in tension with ours. It’s clear China and Russia want to … use their veto powers to promote their own interests at the expense of America and its allies,” he said.

Mattis, in an apparent shot at Trump’s lack of military service or experience, highlighted his own service.

“My views on treating allies with respect and also being clear-eyed about both malign actors and strategic competitors are strongly held and informed by over four decades of immersion in these issues,” he wrote.

Trump got several deferments to avoid the military in Vietnam, including at least one for bone spurs in his foot.

Sen. Lindsey Graham, a vocal opponent of Trump’s Syria bugout, said earlier Thursday that Mattis had opposed the move, but Trump announced it anyway, angering his defense secretary.