THE city of Marawi, President Rodrigo Duterte declared on October 17th, has been liberated from the jihadists who had seized control of it. Mr Duterte was speaking in Marawi a day after Philippine forces had killed Isnilon Hapilon, a leader of Abu Sayyaf, a local militant group, who had declared allegiance to Islamic State (IS). That, in effect, puts an end to the attempt by IS to establish a South-East Asian outpost of its collapsing caliphate. But it took five months of fighting for the army to regain control of the city. And there is a risk that disaffected Filipino Muslims will redirect their aggression into terrorism and extortion, which have racked the southern island of Mindanao, in particular, for decades.

The day after Mr Hapilon’s death, the government said the battle for Marawi had killed 847 IS fighters, 163 soldiers or policemen and 47 civilians. It has emptied the city and surrounding areas, displacing nearly 1.1m people. The fighting has left few buildings in the city centre undamaged and many destroyed. And the lawlessness it suggested has alarmed allies and neighbours.

IS made a determined effort to take Marawi, the biggest city with a Muslim majority in the Philippines, which is largely Catholic. Fighters belonging to Mr Hapilon’s branch of Abu Sayyaf and to another band of Muslim militants, called the Maute group, showed themselves in the city in May. The Filipino fighters were reinforced by armed foreigners and financed by IS.

The attackers infiltrated Marawi in strength and stealthily stockpiled munitions before a single shot was fired. But the security forces detected the presence of Mr Hapilon and tried to arrest him, flushing his fighters from cover. The jihadists quickly fell back to the city centre, allowing most of the population of 200,000 to flee. Mr Hapilon may have assumed that Mr Duterte’s frequent vituperation of America would mean that American forces would not support their Filipino allies. If so, he was wrong.

Mr Duterte imposed martial law on Mindanao and poured troops into Marawi. His soldiers fought slowly from house to house, impeded by snipers and booby traps. The air force bombed buildings thought to harbour jihadists, causing much destruction. American forces provided discreet surveillance.

The government and Mindanao’s main armed Muslim movement, the Moro Islamic Liberation Front, have struck a peace agreement entailing greater autonomy for some Muslim-majority areas. The deal is meant to end the decades-old Muslim rebellion in Mindanao and reduce the attraction of jihad. The ranks of insurgents opposed to this agreement have been thinned by the fighting in Marawi. Yet many Muslims, embittered by conflict and poverty, remain hostile to the Philippine authorities. It may be hard to recruit them for another futile attempt to capture and hold territory—but the more rewarding and safer pursuits of kidnapping, extortion and bombing may not have lost their appeal.