James Mattis has resigned as US defence secretary, pointing to differences with Donald Trump over the treatment of allies and the US approach to “malign actors and strategic competitors” on the world stage.

His resignation comes a day after Trump announced an abrupt withdrawal of American troops from Syria, and amid reports he has also ordered the return of half the 14,000 troops in Afghanistan as well.

Mattis was reported to have gone to the White House on Thursday afternoon in an effort to persuade Trump to agree to a gradual withdrawal of troops from Syria and to leave a residual force of a few hundred, diplomatic sources said. He also opposed Trump’s plans to halve the US contingent in Afghanistan. His resignation suggests those appeals were rejected.

Mattis said he would stay in office until 28 February, to allow time for a successor to be nominated and confirmed by the Senate. His departure is likely to alarm US allies in Europe who saw Mattis as the embodiment of continuity in US defence policy in an otherwise erratic administration.

My views on treating allies with respect and being clear-eyed about malign actors and competitors are strongly held James Mattis

Mattis had doggedly stressed the importance of allies, and of Nato in particular, in the face of outspoken scepticism from the president. His resignation letter emphasised that theme as well as the need to remain “resolute and unambiguous” in the approach to China and Russia.

“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,” 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.

“Similarly, I believe we must be resolute and unambiguous in our approach to those countries whose strategic interests are increasingly in tension with ours,” he wrote, making clear he was referring primarily to China and Russia.

“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,” Mattis said, before making it clear he no longer believed the president shared those convictions.

“Because you have the right to have a secretary of defence whose views are better aligned with yours on these and other subjects, I believe it is right for me to step down from my position,” he said. The letter offers no words of praise for the president.

Allies in Europe who also have troops in northern Syria operating alongside US special forces were not consulted before Trump made the decision to pull the troops out. The Syrian Defence Forces, which have done most of the fighting against Islamic State in Syria, in partnership with the US, were also not consulted.

The decision was strongly criticised by Republican senators, whose support Trump will need to confirm a successor to Mattis. British and French officials also contradicted Trump’s claim to have defeated Isis in Syria.

The withdrawal was, however, warmly welcomed by Vladimir Putin.

“Donald’s right, and I agree with him,” the Russian president said in a news conference on Thursday.

The Wall Street Journal reported on Thursday that Trump had ordered the drawdown of 7,000 US troops in Afghanistan, half the US military presence there. On the same day, the Pentagon delivered a report to Congress arguing for the effectiveness of US support for the Afghan armed forces.

The Journal’s report was later confirmed by CNN.

Trump was the first to break the news of Mattis’s resignation, minutes before the Pentagon sent out the defence secretary’s letter. The president made no reference to policy differences.

“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,” Trump tweeted.

“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 Defense will be named shortly. I greatly thank Jim for his service!”

When he hired Mattis, Trump was impressed by the marine veteran’s bearing and his nickname “Mad Dog”, but the relationship cooled as Mattis resisted Trump’s inclination to distance the US from its Nato allies and blocked bellicose gestures aimed at North Korea in 2017.

“I think he’s sort of a Democrat, if you want to know the truth,” Trump told a TV interviewer in October.

Former defence officials had said that Mattis saw his primary loyalty to the US constitution, and that had guided his relations with an erratic and acerbic president. His resignation means there is no senior member of the Trump administration with a record of standing up to the president.

The former CIA director, John Brennan, said it was now up to Trump’s party to constrain him.

“Okay, Republicans. How much longer are you going to let this farcical ‘presidency’ continue?” Brennan asked on Twitter. “At a time of such political, economic, and geo-strategic turbulence – both nationally and globally – are you waiting for a catastrophe to happen before acting? Disaster looms!”

The Senate majority leader, Mitch McConnell, who very rarely challenges the president, issued an uncharacteristically stern statement suggesting the chamber would only accept a replacement defence secretary in Mattis’s mould.

He said: “I am particularly distressed that he is resigning due to sharp differences with the president on these and other key aspects of America’s global leadership.”

McConnell urged the president “to select a leader who shares Secretary Mattis’s understanding of these vital principles and his total commitment to America’s service members.”

In his bestselling book Fear, the veteran journalist Bob Woodward depicted Mattis blocking Trump’s wilder suggestions – including a mooted assassination of Syrian president Bashar al-Assad – and telling aides the president had the policy understanding of a 10- or 11-year-old child. Mattis denied Woodward’s version of events, leading Woodward to say he was “not telling the truth”.

On Thursday, Mark Warner of Virginia, the senior Democrat on the Senate intelligence committee, wrote on Twitter: “This is scary. Secretary Mattis has been an island of stability amidst the chaos of the Trump administration.”