WASHINGTON — Defense Secretary Jim Mattis abruptly said he was resigning Thursday after two years of disagreements with President Donald Trump over America's role in the world — the announcement coming one day after Trump overruled his advice against pulling troops out of Syria and pressed forward on discussions to withdraw forces from Afghanistan.

Mattis, perhaps the most respected foreign policy official in Trump's administration, will leave by the end of February after two tumultuous years struggling to soften and moderate the president's hardline and sometimes sharply changing policies. He told Trump in a letter that he was leaving because "you have a right to have a Secretary of Defense whose views are better aligned with yours."

Trump said in a tweet that Mattis was retiring, but that's not what Mattis said.

His departure was immediately lamented by foreign policy hands and lawmakers on both sides of the aisle, who viewed the retired Marine general as a sober voice of experience in the ear of a president who had never held political office or served in the military. Even Trump allies expressed fear over Mattis' decision to quit, believing him to be an important moderating force on the president.

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

The announcement came a day after Trump surprised U.S. allies and members of Congress by announcing the withdrawal of all U.S. troops from Syria, and as he continues to consider shrinking the American deployment in Afghanistan. It coincided with domestic turmoil as well, Trump's fight with Congress over a border wall and a looming partial government shutdown.

Trump's decision to pull troops out of Syria has been sharply criticized for abandoning America's Kurdish allies, who may well face a Turkish assault once U.S. troops leave, and had been staunchly opposed by the Pentagon.

Mattis, in his resignation letter, emphasized the importance of standing up for U.S. allies — an implicit criticism of the president's decision on this issue and others.

"While the U.S. 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," Mattis wrote.

Last year, Republican Sen. Bob Corker — a frequent Trump critic — said Mattis, along with White House chief of staff John Kelly and then-Secretary of State Rex Tillerson, was helping "separate our country from chaos."

Tillerson was fired early this year. Kelly is to leave the White House in the coming days.

"This is scary," reacted Senate Intelligence Committee Vice Chairman Mark Warner, D-Va., on Twitter. "Secretary Mattis has been an island of stability amidst the chaos of the Trump administration."

Mattis' departure has long been rumored, but officials close to him have insisted that the battle-hardened retired Marine would hang on, determined to bring military calm and reason to the administration's often chaotic national security decisions and soften some of Trump's sharper tones with allies.

Opponents of Mattis, however, have seen him as an unwanted check on Trump.

Mattis traveled to the White House on Thursday afternoon to inform Trump of his decision to leave the administration. Trump said a replacement would be chosen soon.

"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 press secretary Sarah Sanders.