US Defense Secretary Jim Mattis will leave his job at the end of February, President Donald Trump has announced.

In a post on Twitter, Mr Trump said that Mr Mattis will be "retiring, with distinction".

A retired Marine Corps general, he was known as a stabilising force in Mr Trump's cabinet.

The departure of Mr Mattis had been anticipated since Mr Trump announced yesterday that he was withdrawing US troops from Syria, despite opposition from US allies and his own top military officials.

Mr Mattis said in his resignation letter that he was stepping down so that the President could have a defence chief whose views align more closely with his own.

"Because you have the right to have a Secretary of Defense whose views are better aligned with yours," Mr Mattis said in the letter, "I believe it is right for me to step down from my position."

He hailed the coalition to defeat the so-called Islamic State group and also defended NATO, the military alliance between North America and Europe whose cost-effectiveness has been questioned by Mr Trump.

"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," Mr Mattis wrote.

Mr Trump said he would nominate a successor to Mr Mattis shortly.

Mr Mattis joins a long list of former Trump administration senior figures who have either quit or been removed, some unceremoniously like Secretary of State Rex Tillerson, who Mr Trump fired via Twitter in March.

Mr Trump's White House has had the highest turnover of senior-level staff of the past five presidents, according to the Brookings Institution think tank.

Speculation that Mr Mattis might not last long in his post grew in October when Mr Trump said in a CBS interview that the general was "sort of a Democrat" and might be leaving.