James Mattis has quit as US defence secretary, saying that Donald Trump needs someone in the position with “views better aligned” with those of the president.

The abrupt resignation comes a day after Mr Trump announced that he was pulling US troops out of the conflict in Syria and as the president is reportedly weighing up a substantial withdrawal of forces from Afghanistan.

Mr Mattis is said to have offered his resignation during a meeting on Thursday. The defence secretary is the latest high-profile exit from the Trump administration since the midterm elections last month, and reflects what has been seen as a cooling of relations between the two men since Mr Trump took office.

Mr Trump tried to paint Mr Mattis leaving as a retirement in a tweeted announcement, but it is clear from Mr Mattis’ resignation letter that there were deep differences in opinion.

“General Jim Mattis will be retiring, with distinction, at the end of February, after having served my Administration as Secretary of Defence for the past two years,” Mr Trump tweeted.

He continued: “During Jim’s tenure, tremendous progress has been made, especially with respect to the purchase of new fighting equipment. 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 Defence will be named shortly. I greatly thank Jim for his service”!

In his own letter announcing his retirement, Mr Mattis suggested that he and the president do not see eye-to-eye on foreign policy initiatives, and appeared to rebuke Mr Trump’s diplomatic style that has been viewed as brash and insulting worldwide. “Because you have the right to have a Secretary of Defence whose views are better aligned with yours ... I believe it is right for me to step down from my position,” he said.

Mr Mattis has reportedly disagreed with the president’s decision to move forward with the plan to withdraw troops from Syria, and had argued that the US counterterrorism mission in the country is not yet over. He had tried to tell Mr Trump that withdrawing the American presence from the country would only lead to more chaos in the Middle East.

“The president’s national security team’s job is to give him advice and it’s the president’s job to make a decision,” said White House press secretary Sarah Sanders.

The decision to withdraw has been criticised for abandoning America’s Kurdish allies, who may face an assault from Turkey once the buffer of US troops is removed. In his resignation letter, Mr Mattis emphasised the importance of standing up for allies in an implicit criticism of the president.

“One core belief I have always held is that our strength as a nation is inextricably linked to the strength of our unique and comprehensive system of alliances and partnerships,” Mr Mattis wrote. “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.”

As well as making clear the importance of America’s allies, Mr Mattis called for Mr Trump to be ”resolute and unambiguous” with nations like China and Russia who “want to shape a world consistent with their authoritarian model”.

“That is why we must use all the tools of American power to provide for the common defence,” Mr Mattis wrote.

The four-star Marine Corps general was once considered to have been one of the most trusted advisers to Mr Trump, who did not serve in the military and had no government experience prior to becoming president.

However, 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. Opponents of Mr Mattis have seen him as an unwanted check on Mr Trump.

Just recently, Mr Trump appointed General Mark A Milley – the US Army’s chief of staff – to replace General Joseph F Dunford Jr as chairman of the Joint Chiefs of Staff. Mr Mattis is said to have preferred Air Force chief of staff General David L Goldfein to take the role.

Mr Mattis’ departure was quickly lamented by both Republicans and Democrats in Congress. “This is scary,” Senate Intelligence Committee vice chairman Mark Warner – a Democrat from Virginia – said on Twitter. “Secretary Mattis has been an island of stability amidst the chaos of the Trump administration.”

“Just read Gen Mattis resignation letter,” tweeted Republican Florida Senator Marco Rubio. “It makes it abundantly clear that we are headed toward [sic] a series of grave policy errors which will endanger our nation, damage our alliances & empower our adversaries.”

Republican Lindsay Graham, a member of the Senate Armed Services Committee, who has opposed the withdrawal of troops in both Syria and Afghanistan said it was with “great sadness” that he learned of Mr Mattis’ resignation.

Democrat leader in the Senate, Chuck Schumer said that the “few symbols of strength and stability” in the Trump administration are all leaving the White House and other departments.

Support free-thinking journalism and attend Independent events

Last year, Republican Senator Bob Corker – a frequent Trump critic – said Mr Mattis, along with White House chief of staff John Kelly and then secretary of state Rex Tillerson, were helping “separate our country from chaos”. Mr Tillerson was fired early this year. Mr Kelly is to leave the White House in the coming days.

At the start of the Trump administration, the president had gushed about his respect for Mr Mattis, repeatedly calling him “Mad Dog”, despite Mattis’ own public insistence that the moniker was never his. Instead, his nickname for years was Chaos, which stood for “Colonel Has An Outstanding Suggestion,” and reflected Mr Mattis’ more cerebral nature.

They clashed on policy issues quickly however, with Mr Mattis saying torture does not work, despite Mr Trumps assertion during the 2016 presidential campaign that it did, and Mr Mattis also voiced staunch support for traditional US international alliances, including Nato, which the president has repeatedly criticised. That theme was continued in his resignation letter.

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.