Deborah Haynes, foreign affairs editor

Don't be fooled into a false sense of security by headlines about the imminent defeat of Islamic State as a holder of territory in Syria and Iraq.

That fight - while long, bloody and dangerous - was the easy bit.

What happens next will determine whether the US-led campaign against the terrorist group will succeed.

Islamic State (IS) is an ideology much more than a physical caliphate.

Members and supporters still number in the tens of thousands, scattered around the globe and able to conduct attacks.

And the IS presence online - while significantly muted thanks to the efforts of British, US and other coalition cyber operations - is still a threat.

It will be all too tempting for politicians - as US President Donald Trump has already demonstrated - to declare victory against the group in Syria and withdraw as the last square feet of territory fall to Syrian Democratic Forces, supported by US, UK and other militaries.

Image: Donald Trump said the IS caliphate in Iraq and Syria is 'ready to fall'

This is effectively what happened in Iraq back in 2009 and 2010 following battlefield and reconciliation successes over the previous three years against a precursor group to IS called al Qaeda in Iraq (AQI), as well as other Sunni Islamist affiliates.

As soon as the physical number of AQI attacks - the suicide bombings, kidnappings, torture and other atrocities - dropped, the political support in Washington and London for investing so many troops, diplomats and dollars on tackling the threat waned.

The UK government under then-prime minister Gordon Brown was already completing Tony Blair's pledge to pull out the majority of British servicemen and women.

But Barack Obama also followed suit after failing to seal an agreement as president with the Iraqi government to enable US troops to stay longer on the ground.

American money that had funded programmes to give disenfranchised members of Iraq's Sunni minority - who had fed the ranks of the insurgency that helped create AQI - dried up.

A US and British ambition for the Shia-led government of then-prime minister Nouri al Maliki to continue these payments and fold the Sunni militias into the regular security forces failed.

This left many of these Sunni fighters sidelined, jobless or working without pay and increasingly bitter.

Image: Member of Iraq's army take photos with an IS flag claimed after fighting

There was also still a hard core of militants, including foreign fighters, who had never been rehabilitated but had avoided capture or death.

They still wanted to unleash sectarian carnage but were biding their time, watching as the discontent within their own Sunni communities grew.

US and British officials in Iraq at the time could see this was happening and knew of the dangers of allowing the breakdown to fester.

But Iraq was no longer the main effort. Washington and London were no longer interested, distracted by the fight that had ramped up against the Taliban in Afghanistan and keen to consign the unpopular intervention against Saddam Hussein to the history books.

Big mistake.

Within a mere four years Islamic State had formed and ridden into the sprawling northern city of Mosul with barely any resistance. From there, their influence and hold on territory grew along with the violence and terror.

We are approaching a similar way-point now.

I am not advocating a permanent Western military presence on the ground in Iraq and northern Syria.

But an over-hasty exit dictated by political timelines instead of conditions on the ground - as Mr Trump has ordered - will again create a security vacuum.

There must also be an increased non-military focus on genuinely helping communities rebuild and rehabilitate after years of war and sectarian conflict.

In Iraq, a failure by the government in Baghdad to curb sectarian attacks and the marginalisation of the Sunni population will create the same cycle of disenfranchisement, leading to fresh violence.

I am not advocating a permanent Western military presence on the ground in Iraq and northern Syria. But an over-hasty exit dictated by political timelines instead of conditions on the ground - as Mr Trump has ordered - will again create a security vacuum.

Jeremy Hunt, the foreign secretary, told parliament last week "there are already concerns being expressed" that IS is beginning to re-establish new territorial footholds in parts of the country - a truly sorry indictment of the efforts so far to win the peace.

In Syria, the situation is even more complex given the simultaneous final stages of the civil war between President Bashar al Assad and a dwindling opposition, placing in doubt the future of the territory that once fell under the IS caliphate.

If Syrian Kurds are allowed to hold onto this land - something that would require an accommodation with Assad and the persuasion of Turkey - there is a chance stability could be created.

Finally, there is the question of what to do about the hundreds of captured foreign fighters who are being held by Kurdish forces in northern Syria, including Britons, as well as their wives and children, among them the heavily pregnant British teenager Shamima Begum.

Despite the national security risks and legal challenge, a failure by Britain and its allies to process their respective citizens - whether through the criminal justice system or de-radicalisation programmes - would be a violation of the values that underpin our democracies.

It would also mean a missed opportunity to try to tackle and better understand the ideology that inspired these people to join the caliphate in the first place.

Sky Views is a series of comment pieces by Sky News editors and correspondents, published every morning.

Previously on Sky Views: Adam Boulton - Like it or not, here's why I don't make deals with interviewees