KABUL, Afghanistan — The Islamic State’s main stronghold in eastern Afghanistan collapsed in recent weeks, according to American and Afghan officials, following years of concerted military offensives from American and Afghan forces and, more recently, the Taliban.

President Ashraf Ghani recently claimed that the Islamic State, often known as ISIS, had been “obliterated” in Nangarhar Province, the group’s haven in the east. And in an interview in Kabul on Sunday, Gen. Austin S. Miller, the commander of all American and NATO forces in Afghanistan, said the group’s loss of the terrain it stubbornly held for few years would severely restrict their recruitment and planning.

But General Miller also warned that ISIS could remain a threat in Afghanistan even if it does not hold territory, with attention required to track militants on the move and the group’s remaining urban cells.

“It was instructive in Iraq and Syria — when you take away big terrain from them, they move into smaller cells and they pop up in strange places,” General Miller said.