The failure of the attempted coup in Turkey is a welcome development. A NATO nation and a European Union candidate country shouldn’t be ruled by its military, even if that institution sees itself as a guarantor of secularism.

But the botched putsch doesn’t mean good news is ahead for Turkey. Or for E.U.- and U.S.-Turkish relations, which could mean setbacks for the multicountry campaign against the Islamic State of Iraq and the Levant, since Turkey is crucial to the anti-ISIL coalition. So now is the time for deft diplomacy to keep Turkey’s convulsions from further destabilizing geopolitics.

For Turkey, “there is no good outcome,” Soner Cagaptay, a senior fellow at the Washington Institute for Near East Policy, told an editorial writer. “If the military would have won, the country would have become oppressive. But Erdogan won, and it will still become more oppressive.”

In fact, Turkish President Recep Tayyip Erdogan has unleashed a sweeping purge of perceived enemies, including 35,000 members of the military, judiciary and police as well as more than 15,000 Education Ministry workers and 1,577 deans, and he has revoked the licenses of 21,000 private schoolteachers. This comes after years of harassing political opponents, civil society, the news media and other targets. While arrests of some in uniform are to be expected after a coup attempt, the extent of Erdogan’s actions suggests deeper political motives.

Erdogan’s top target is Fethullah Gulen, an erstwhile Erdogan ally now living in self-exile in Pennsylvania. Erdogan accuses Gulen and his Turkish followers of being behind the coup attempt and reportedly will press the U.S. to extradite the cleric to Turkey for trial.

Extradition is a legal process tied to treaty-based agreements, so the U.S. should follow the law if and when a formal request comes from Ankara. The Obama administration also should urge restraint and a refocus on fighting ISIL. E.U. leaders must amplify this message and not allow a recent deal between the union and Turkey regarding refugees and other migrants to compromise E.U. democratic ideals.

Turning Turkey back to fighting ISIL won’t be easy. The Turkish military is “severely traumatized,” having lost significant elements of its second-tier leadership, according to Ross Wilson, former U.S. ambassador to Turkey who is now a distinguished fellow at the Atlantic Council.

Additional complicating factors include U.S. concerns over authoritarianism, which already has strained ties. And “to the extent Erdogan makes Gulen the alpha-omega of U.S.-Turkish relations and this other actor [ISIL] gets a vote, our ability to work together, at least for a while, will be a lot harder.”

Indeed, for the U.S. and the anti-ISIL coalition, the coup “could not have come at a worse time,” Cagaptay told an editorial writer.

But now that the worst of times has arrived, President Obama and his successor should prod Turkey to act like the democracy it claims was saved from coup plotters and to refocus on the essential fight against ISIL.