Americans who fought in Iraq are troubled to see chaos and violence again engulfing their old battleground. (Ernesto Londoño)

— Col. Samuel Whitehurst had been consumed with work in the last days of his brigade’s nine-month deployment in eastern Afghanistan when alarming news about his former battleground in northern Iraq began to reach him.

Whitehurst fired off an e-mail to the mayor of the Iraqi city of Samarra, who had become a close friend, saying he was thinking of him. Days after a band of Islamic militants took over Mosul and several towns in the north in early June, he got terrible news: Col. Gayath Sami, the Iraqi officer Whitehurst had groomed to run Samarra’s security command center, had been slain in the fighting.

“To find out that he had been killed,” said Whitehurst, who commands the 3rd Brigade Combat Team of the 10th Mountain Division, deployed in Paktia province. “Those are the things I worry about — the friends I met there and what’s going to happen to them.”

The catastrophic turn Iraq has taken in recent weeks has startled U.S. veterans who spent years seeking to set up the country, and particularly its security forces, for success. The Iraq war killed nearly 4,500 U.S. troops and, by some estimates, cost taxpayers more than $2 trillion dollars. The country’s violent downward spiral as Islamist militants seized large swaths of territory has been particularly unsettling to those who are currently in Afghanistan at the tail end of America’s longest war and hoping for a better outcome.

“Watching how much everybody worked to continue to have hope and progress for that country and to watch it crumble is fairly disheartening,” said Lt. Gen. Joseph Anderson, who spent 42 months in Iraq and is now tactical commander of U.S. and allied troops here. “It’s very personal when you get letters and notes and e-mails from all the people you know there who used to work for you saying: ‘Can you get my family out of there, everything is collapsing.’ ”

Drawing parallels between Iraq and Afghanistan and the aftermath of the American invasion in each has always been difficult, but some similarities are unmistakable.

As they did in Iraq, U.S. military officials speak optimistically about the growing competence and strength of the security forces in Afghanistan that Washington has spent billions of dollars to train and equip. Echoing assessments about the strength of the Sunni insurgency in Iraq during the final year of the U.S. military presence there, commanders here assert that the Taliban movement has been markedly weakened and no longer controls important urban areas.

In some ways, Iraq’s outlook seemed brighter as U.S. troops were making their exit. Its vast oil reserves were again luring foreign investment and propelling economic growth. Using its own funds, the government in Baghdad placed orders for advanced U.S. military equipment worth billions of dollars. The country’s leaders felt confident enough about the post-American era that they balked at authorizing legal immunity for a small contingent of military advisers the Obama administration sought to keep after the combat mission ended in December 2011.

“It was set to be a huge success,” Maj. Rob Wolfenden, 37, who is leading the effort to shut down a base in southern Afghanistan, said, recalling his optimism at the end of his second deployment in Iraq in 2008. “The oil was pumping. The Iraqi forces were completely solid. They knew how to fight, and they could fight.”

As his current base, Forward Operating Base Pasab in Kandahar province, which was once home to some 5,000 troops, he has watched developments in Iraq with dismay.

“It’s a shame to see Iraq now,” the major said. “I’m bothered to see this lack of security.”

Top commanders here have noted that the new Afghan president is all but certain to sign a security cooperation agreement with Washington, provided the dispute over alleged widespread voting fraud in the recent presidential election is settled and the new leader takes office soon. That would keep a small coalition of international troops here beyond the end of the year and, perhaps more critically, pave the way for continued foreign aid for Afghanistan for years to come.

In Iraq, shortly after U.S. troops left, many military and security officers who were groomed and championed by the Americans were sidelined or forced out as Prime Minister Nouri al-Maliki filled the ranks with loyalists. The shift exacerbated sectarian tensions as the country’s security forces became increasingly dominated by Shiites and their Sunni colleagues complained they were being treated with contempt.

In the wake of Afghanistan’s June 14 runoff presidential election, U.S. officials are anxiously watching the extent to which commanders here cast their lots with each of the candidates vying to replace Hamid Karzai. The potential politicization of the security forces has become a matter of growing concern as one of the candidates, Abdullah Abdullah, has called for protests after alleging that his rival’s campaign rigged the vote. So far, U.S. commanders say they think it’s not likely that Afghanistan’s security forces could splinter along ethnic lines to the degree Iraq’s did along sectarian lines.

“It’s pretty clear in Iraq a Shiite leader did some pretty serious purging,” Anderson said. “I don’t see the rifts [in Afghanistan] anywhere near as much as I did in Iraq. They have done a better job in terms of having an integrated force.”

Some American soldiers in Afghanistan said the aftermath of the U.S. pullout in Iraq shouldn’t be seen as a failed military campaign but rather a case study of the limitations of what the armed forces can accomplish.

“Personally, I don’t feel anything I endured in Iraq was in vain,” said Capt. Rick McCuan, 31, an intelligence analyst with the 82nd Airborne Division who is deployed in southern Afghanistan. “We were charged with a mission, and we executed that mission to set the conditions for the Iraqi people to take control of their own destiny.”

Others can’t help but feel a degree of bitterness. Staff Sgt. Kenneth Ventrice, 34, of Boston, served three tours in Iraq and is on his second in Afghanistan.

“It affected me personally because I was in Fallujah,” he said on a recent morning during a lull in a security patrol in Zabul province. “The blood, sweat and tears we spent trying to make that place better.”

Anderson said he has sought to keep his feelings regarding Iraq in check.

“I can’t get overwhelmed by it, but it is very frustrating, and it does make you more determined here to make sure that we keep doing everything we can,” he said. “You have to be the eternal optimist.”