By ZEKE MILLER and LOLITA BALDOR , Associated Press

WASHINGTON (AP) — Defense Secretary Jim Mattis abruptly said he was resigning Thursday, a day after President Donald 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.

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.

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.

His departure was quickly lamented by foreign policy hands on both sides of the aisle, who viewed him as a sober voice of experience in the ear of a president who had never before held political office or served in the military.

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

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.

A White House official said Mattis informed Trump of his decision to leave the administration Thursday afternoon. Trump said a replacement would be chosen soon.

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 Trumps 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 initially divided on the future of the Afghanistan war, with Trump complaining 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.

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.

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.

More From KTEM-AM