Defense Secretary Jim Mattis resigned Thursday after clashing with President Donald Trump over the abrupt withdrawal of U.S. troops from Syria and after two years of deep disagreements over America's role in the world.

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."

Mattis went to the White House with his resignation letter in hand to meet with the president and spoke to Trump for about 45 minutes, according to a senior U.S. official familiar with the incident but speaking on conditions of anonymity to discuss a private meeting.

There was no confrontation between the two men, the official said, and there was no one issue that caused the resignation. However, the official said, Syria likely was the last straw for Mattis.

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."

Mattis did not mention the dispute over Syria in his letter or proposed deep cuts to U.S. forces in Afghanistan, another significant policy dispute. He noted his "core belief" that American strength is "inextricably linked" with the nation's alliances with other countries, a position seemingly at odds with the "America First" policy of the president.

The defense secretary also said China and Russia want to spread their "authoritarian model" and promote their interests at the expense of America and its allies. "That is why we must use all the tools of American power to provide for the common defense," he wrote.

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 cutting in half the American deployment in Afghanistan by this summer. The news 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, were 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."

"Jim Mattis did a superb job as Secretary of Defense. But he cannot be expected to stand behind a President who disrespects our allies and ingratiates himself to our adversaries," said William Cohen, who served as defense secretary under Bill Clinton and knows Mattis well.

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 judgment to the administration's often chaotic national security decisions and to soften some of Trump's sharper tones with allies.

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

Mattis went to the White House Thursday afternoon to resign after failing to persuade the president in a tense Oval Office meeting to change his decision on withdrawing troops from Syria, according to two people with knowledge of the conversation but not authorized to discuss it publicly.

Another U.S. official said that Mattis' decision was his own, and not a "forced resignation." The official spoke on condition of anonymity to discuss internal deliberations.

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.

At the start of the Trump administration, the president had gushed about his respect for 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 Mattis' more cerebral nature.

The two quickly clashed on major policy decisions.

During his first conversations with Trump about the Pentagon job, Mattis made it clear that he disagreed with his new boss in two areas: He said torture doesn't work, despite Trump's assertion during the campaign that it did, and he voiced staunch support for traditional U.S. international alliances, including NATO, which Trump repeatedly criticized.

Mattis was credited by some in the administration for blocking an executive order that would have reopened CIA interrogation "black sites." Trump has said the Pentagon chief convinced him it wasn't necessary to bring back banned torture techniques like waterboarding.

En route to his first visit to Iraq as defense secretary, Mattis bluntly rebuffed Trump's assertion that America might take Iraqi oil as compensation for U.S. efforts in the war-torn country.

The two also were divided on the future of the Afghanistan war, with Trump complaining from the first about its cost and arguing for withdrawal. Mattis and others ultimately persuaded Trump to pour additional resources and troops into the conflict to press toward a resolution.

U.S. officials say there now is active planning in the Pentagon that would pull as many as half the 14,000 U.S. troops out of Afghanistan by summer. They say no final decision has been made.

Trump also chafed at the Pentagon's slow response to his order to ban transgender people from serving in the military. That effort has stalled due to multiple legal challenges.

More recently, Trump bypassed Mattis' choice for the next chairman of the Joint Chiefs of Staff. Gen. David Goldfein, the Air Force chief, was Mattis' top choice, but Trump chose Gen. Mark Milley, the chief of the Army.

The Pentagon has appeared to be caught off guard by a number of Trump policy declarations, often made through Twitter. Those include plans that ultimately fizzled to have a big military parade this month and the more recent decision to send thousands of active duty troops to the Southwest border.

Mattis has determinedly kept a low public profile, striving to stay out of the news and out of Trump's line of fire.

Those close to him have repeatedly insisted that he would not quit, and would have to either be fired or die in the job. But others have noted that a two-year stint as defense chief is a normal and respectable length of service.

Born in Pullman, Washington, Mattis enlisted in the Marine Corps in 1969, later earning a history degree from Central Washington University. He was commissioned as an officer in 1972. As a lieutenant colonel, he led an assault battalion into Kuwait during the first U.S. war with Iraq in 1991.

In the aftermath of the 9/11 attacks, Mattis commanded the Marines who launched an early amphibious assault into Afghanistan and established a U.S. foothold in the Taliban heartland. As the first wave of Marines moved toward Kandahar, Mattis declared, "The Marines have landed, and now we own a piece of Afghanistan."

Two years later, he helped lead the invasion into Iraq in 2003 as the two-star commander of the 1st Marine Division. As a four-star, he led Central Command from 2010 until his retirement in 2013.

__

Associated Press writers Jill Colvin and Robert Burns contributed.