President Biden on Monday announced that the United States will wrap up its combat mission in Iraq by year’s end, his latest effort to push American diplomacy past a post-9/11 worldview and shift its focus away from terrorism and the Middle East and toward threats like China and cyberwarfare.

Welcoming Iraqi Prime Minister Mustafa al-Kadhimi to the Oval Office, Biden promised ongoing support for democracy in Iraq, including elections this fall, but he said the military mission there will change.

“Our role in Iraq will be . . . just to be available to continue to train, to assist and to help, and to deal with ISIS as it arrives, but we’re not going to be, by the end of the year, in a combat mission,” Biden said, referring to the Islamic State terrorist group.

The Iraq announcement came three months after Biden announced the full withdrawal of U.S. troops from Afghanistan and just one week after he started transferring prisoners from Guantánamo Bay in hopes of eventually shutting down the prison.

Taken together, the moves represent what has become a pillar of Biden’s foreign policy: ending two decades of what he sees as an outdated reaction to the Sept. 11, 2001, terrorist attacks and focusing on an increasingly aggressive China, which he sees as the biggest threat to American security.

By relegating U.S. forces in Iraq to the back seat, Biden is trying to draw the curtain on the costliest, deadliest conflict of the era that past presidents called the “global war on terrorism.” Some 18 years after it began, the Iraq War is now a deeply unpopular chapter of American foreign policy.

It also has an acutely painful legacy for the president himself. Beau Biden, who died in 2015, served as a reservist in Iraq, and the president has said that he suspects toxins from that deployment led to his son’s fatal brain cancer.

For his part, Kadhimi is seeking an ongoing U.S. military presence to help Iraq’s government repel the remnants of ISIS, but he agreed Monday that American forces should not be involved in front-line combat.

“Mr. President, I thank you for all the blood and treasure that America has given for a free and democratic Iraq,” Kadhimi said in English.

An Iraqi delegation began meeting with officials at the Pentagon and State Department last week to discuss how the United States can maintain economic, humanitarian and cultural ties to Iraq even as the combat mission ends.

Gil Barndollar, a former Marine who served two tours in Afghanistan, said it’s past time to wind down the war machinery of the post-9/11 era.

“It’s overdue,” said Barndollar, a senior fellow at the Defense Priorities think tank. “We have something like four times the number of jihadist groups as there were on 9/11. The global war on terror has failed by just about any measure.”

But the trauma of the terrorist attacks two decades ago — which came out of a clear blue sky to kill nearly 3,000 Americans — has not faded, and some warn against turning the page prematurely. Sen. Lindsey O. Graham (R-S.C.) has called Biden’s approach “a disaster in the making” that risks a resurgence of al-Qaeda and the Islamic State.

There are signs that a clean break will not be easy. The Taliban has made significant gains in Afghanistan since the United States began pulling out. The American diplomats who remain there face significant challenges. Biden continues grappling with whether Iran, a key Middle East adversary, can be coaxed back into a nuclear deal.

In Iraq, Biden faces pressure to do more against Iranian-backed attacks.

Natasha Hall, a senior fellow at the Center for Strategic and International Studies, said Iran’s growing influence is one of several reasons Biden’s shift might not go as planned. “If history is any guide, the U.S. risks getting sucked back into the Middle East for a range of reasons, including but not limited to terrorism,” Hall said.

More than 4,000 U.S. troops were killed in a war that began with a U.S.-led invasion in 2003, the vast majority before 2011, when the U.S. military formally declared an end to the war. A smaller counterterrorism mission resumed in 2014, and that is what is now being downshifted.

It is not yet clear whether the current force of about 2,500 will get much smaller. The United States had about 170,000 troops in Iraq in 2007, at the height of the U.S.-led war against al-Qaeda and other militant groups.

“No one is going to declare ‘Mission accomplished,’ ” a senior Biden administration official said ahead of the meeting. That was the woefully premature slogan associated with President George W. Bush, who delivered an ill-fated 2003 speech before a banner proclaiming it.

Still, the official said Monday’s announcement was a pivotal point in the conflict.

“It is an important moment to reflect on how far we’ve come,” and on the sacrifices by Iraqis, Syrians and Americans, said the official, who spoke on the condition of anonymity to discuss strategy. “And so it’s important. I think it’s poignant.”

For Biden, the persistent U.S. presence and the ongoing risk to U.S. troops make no sense in Afghanistan and only a little in Iraq at a time when an increasingly powerful, wealthy and aggressive China is on the rise.

“We went to Afghanistan because of a horrific attack that happened 20 years ago,” Biden said in April, when he announced that U.S. withdrawal. “That cannot explain why we should remain there in 2021.”

Biden originally supported the war in Iraq, and he also backed the invasion of Afghanistan at first. As a senator, he attended a 2002 ceremony at the White House when Bush signed a bill authorizing the use of force against Iraq, after accusing its then-leader, Saddam Hussein, of amassing weapons of mass destruction. No such weapons were ever found.

Just last month, the House voted to repeal that 19-year-old permission slip, which critics have argued was misused and is now outdated. Biden gave his blessing to the move.

Like many Americans, Biden’s views on the wars in Iraq and Afghanistan changed the longer the conflicts dragged on — especially the invasion of Iraq, which had little or nothing to do with Sept. 11. By 2005, he was calling his Iraq vote a mistake.

As vice president, Biden argued against the troop surge that President Barack Obama ordered in Afghanistan in 2009. And now that he is president himself, Biden has taken to invoking the adage about the futility of doing the same thing repeatedly and expecting a different result.

“I’m now the fourth United States president to preside over American troop presence in Afghanistan — two Republicans, two Democrats,” Biden said in April. “I will not pass this responsibility on to a fifth.”

He is also hewing to his own suspicions that Pentagon leaders will always argue to stay in a fight or add forces to it, said people who have spoken to Biden about Afghanistan and Iraq.

Obama’s new memoir records Biden as issuing strong advice about the brass.

“‘Maybe I’ve been around this town for too long, but one thing I know is when these generals are trying to box in a new president,’ ” Obama says Biden told him. “ ‘Don’t let them jam you.’ ”

Biden has ordered a global “posture review” of U.S. military deployments, “so that our military footprint is appropriately aligned with our foreign policy and national security priorities.”

Monday’s meeting with Kadhimi followed a similar session last month with Afghan President Ashraf Ghani, in which Biden was matter of fact about the U.S. imperative to leave.

“Afghans are going to have to decide their future, what they want,” Biden said.

The United States will help, he promised, but the support from now on would be largely financial, not military. Biden had announced in April that the remaining 2,500 U.S. forces would be out of Afghanistan by Sept. 11 this year, the 20th anniversary of the terrorist attacks, but the withdrawal is already effectively complete.

The U.S. official said the counterterrorism mission in Syria, a parallel to the one in Iraq, will continue for now.

Although the number of U.S. troops in Iraq is expected to change little, if at all, the formal change in the military posture may create political space for both Biden, who campaigned on ending long-running wars, and Kadhimi, who faces pressure to wind down the American mission. But it may be hard for many people to see a difference.

With the exception of U.S. Special Operations forces, American troops have mostly worked from large Iraqi bases over the past few years, assisting in the collection of intelligence and advising increasingly capable Iraqi forces as they fight the Islamic State.

U.S. defense officials have sought to highlight that U.S. troops remain in Iraq at the invitation of the Iraqi government.

“It’s not about a desire to have a presence or not to have a presence,” John Kirby, a Pentagon spokesman, said last week. “It’s a desire, in concert with our Iraqi partners, to eradicate the threat that ISIS poses.”

Any remaining U.S. troops will probably face continued threats from Iranian-backed Shiite militias that have launched dozens of attacks over the past few years, killing a handful of coalition members. They have come both in the form of rockets and, more recently, explosives-laced drones that are harder to detect.

The Washington Institute for Near East Policy has counted 24 attacks since Biden took office and three U.S. retaliatory strikes.

In comments to reporters Monday evening, Kadhimi indicated he expected the United States, under the non-combat arrangement, to refrain from the missile strikes with which it has responded to such attacks in the past. “We are responsible for protection [against] anything that happens on Iraqi soil,” he said. “We are responsible for the protection of Iraqi guests” who “are there to support us and strengthen our capabilities.”

Obama also tried to redirect U.S. priorities toward Asia, only to be derailed by a war that erupted in Syria on his watch.

Biden’s focus on China has held so far, however, including sanctions and condemnations of human rights abuses in Hong Kong and a continuation of former president Donald Trump’s protectionist tariffs. European leaders were willing to go along with Biden’s tougher stance on China, to a point at least, when he pressed them during his first foreign trip as president in June.

None of this means that confronting China will be easy, given its determination to challenge the United States’ role as the world’s dominant power.

“China is playing hardball. They want very much to be the dominant state,” another senior administration official said in an interview. “They also want to help usher us off the stage in a variety of ways.”

Dan Lamothe and Karen DeYoung contributed to this report.