The U.S. interventions in Afghanistan and Iraq have been on roughly parallel trajectories for two decades.

Both invasions began after the Sept. 11, 2001, attacks (even though there was no connection between the attacks and Saddam Hussein). Both operations resulted in the swift overthrow of tyrannical regimes followed by major difficulties in building successor governments. Both countries saw the growth of substantial insurgencies, which led to U.S. troop surges — in Iraq in 2007 and in Afghanistan in 2009. The surge in Iraq was much more successful than in Afghanistan. It made possible the withdrawal of U.S. troops in 2011. But U.S. forces returned in 2014 to help Iraqi troops battle a growing Islamic State movement.

By the beginning of this year, the two countries again appeared parallel in terms of U.S. involvement: We had roughly 2,500 troops in both countries working primarily in an advisory capacity. But now another major divergence is occurring that makes little sense: U.S. troops are remaining in Iraq but leaving Afghanistan. They need to stay in both countries to prevent a resurgence of the terrorist threat to the United States and its allies.

On Monday, President Biden announced, during a visit to the Oval Office by Iraqi Prime Minister Mustafa al-Kadhimi, that the United States will withdraw its combat forces from Iraq by the end of the year. But don’t be deceived. This doesn’t change much. A few troops might leave, but others will simply be rebranded from “combat” to “advisory” forces. This is an attempt to satisfy nationalist demands, fomented by Iran, for the departure of U.S. forces without actually pulling them out.

The Biden administration knows that U.S. military help is still needed to prevent a resurgence of Islamic State in Iraq (its suicide bombers killed 30 people in a suburban Baghdad market recently), to limit Iranian influence over that strategically important country, and to support U.S. operations against the Islamic State in Syria (where some 900 U.S. troops also remain). So Biden is keeping U.S. forces in Iraq despite the growing risk that they will be rocketed by Iranian-backed militias.

But if Biden is maintaining U.S. troops in Iraq, why is he withdrawing them from Afghanistan — where the need is even greater? The Taliban presents a much larger threat to the government in Kabul than the Islamic State does to the government in Baghdad — and the Islamic State is also present in Afghanistan. Moreover, Afghan forces are much more reliant on U.S. help than their Iraqi counterparts. The pullout from Afghanistan raises the risk of the kind of precipitous collapse that occurred in Iraq during the Islamic State onslaught in 2014.

Gen. Mark A. Milley, chairman of the Joint Chiefs of Staff, admitted last week that the Taliban now controls nearly half of Afghanistan’s districts — 200 out of 419. The previous month, he said that the Taliban controlled only 81 districts. That’s an alarming trend. The Taliban is now massing around major urban centers amid growing fears that they could soon take Kandahar, the country’s second-largest city. The Taliban has already seized many of Afghanistan’s border posts, securing tariff revenue for itself and denying it to the Afghan government.

The Afghan special forces have been performing heroically, and even regular army units have been fighting hard, but poorly equipped, trained and motivated Afghan police units have been unable or unwilling to hold territory. Many Afghan outposts have fallen simply because the government lacks the capacity to keep them supplied by air now that the United States has withdrawn its own aircraft and even the contractors who used to service Afghan aircraft.

The situation has become so grim that U.S. warplanes have again been bombing Taliban positions, particularly around Kandahar, but U.S. generals have said even those operations — which are much harder to undertake now that U.S. aircraft are no longer based in Afghanistan — will end by September. At that point, few would give the Afghan government good odds of survival.

I get that Biden wants to end “forever wars,” disengage from the Middle East and focus on the looming threat from China. But we can still pivot to the Pacific while keeping 2,500 troops in Afghanistan and 2,500 troops in Iraq — together amounting to just 0.4 percent of the military’s active-duty end strength.

Why stay in Iraq while leaving Afghanistan? Biden can cite the pullout agreement signed by the Trump administration, but the Taliban has already abrogated it by failing to break with al-Qaeda. It’s true that Iraq has lots of oil production and Afghanistan doesn’t. But we don’t need Iraq’s oil — and, contrary to Trump’s threats, we’re not going to take it anyway.

The real reason we had troops in Iraq and Afghanistan was to prevent violent Islamist extremists from seizing power and turning those countries into international terrorist bases. That imperative remains in both countries — but in only one of them are we maintaining the kind of military presence needed to keep the extremists at bay.