The coup in Turkey is over, and now the purge begins.

On Saturday, Turkish soldiers and police—those who had remained loyal to President Recep Tayyip Erdoğan during the uncertain hours of the previous day—were rounding up their enemies across the security services, reportedly arresting thousands. There will be thousands more. In the high-stakes world of Turkish politics—nominally democratic but played with authoritarian ferocity—justice for the losers will be swift and brutal.

The remarkable thing about Friday’s coup attempt is not that it failed but that, after years of Erdoğan’s relentless purging of his opposition, there was a faction inside the Turkish military strong enough to mount one at all.

The confrontation was a long time coming. When Erdoğan first became Prime Minister, in 2003, he was the Islamic world’s great democratic hope, a leader of enormous vitality who would show the world that an avowedly Islamist politician could lead a stable democracy and carry on as a member of NATO, too.

Those hopes evaporated quickly. Erdoğan, who was elected Turkey’s president in 2014, has taken a page from Vladimir Putin’s playbook, using democratic institutions to legitimize his rule while crushing his opponents, with an eye to ultimately smothering democracy itself. Over the past decade, Erdoğan has silenced, marginalized, or crushed nearly anyone in the country who might oppose him, including newspaper editors, university professors, aid workers, and dissident politicians. (What an irony that Erdoğan, who has imprisoned so many journalists, and gone to great lengths to censor Twitter, Facebook and YouTube, may have saved his Presidency by using FaceTime to make an early Saturday appearance on a Turkish television news channel.) President Obama and other Western leaders, seeing Erdoğan as a bulwark against chaos, largely gave him a pass. In his most recent grab for authoritarian powers, Erdoğan pushed through a law that stripped members of parliament of immunity from prosecution, a measure that his critics fear, with good reason, that he will use to remove the few remaining lawmakers who still oppose him.

Then there’s the military. Since the Turkish republic was founded, in 1923, the county’s generals have imagined themselves the ultimate arbiters of its politics, stepping into power—sometimes savagely—whenever they felt the government had become either too leftist or too Islamic. (After the military overthrew a democratically elected government in 1960, the generals executed the Prime Minister.) The military has had a special contempt for Erdoğan, whom they regarded as a dangerous Islamist—but they have proven no match for him.

In 2007, Erdoğan’s henchmen initiated a series of show trials, known collectively as Sledgehammer, in which fabricated evidence was used to remove the top tier of the Turkish officer corps. Hundreds were sent to prison, and the military itself seemed banished from politics forever. Indeed, Erdoğan must have been surprised that there was still a dissident faction of the armed forces large enough to try to bring him down. On Friday, the coup’s organizers didn’t even have the sense to detain the man they were trying to overthrow, and they apparently never seriously contemplated shooting their way into the palace. (After a coup in 1980, the military killed and imprisoned tens of thousands.) In the wake of their failure, the military will be soon be under Erdoğan’s total control, like virtually every other institution in the country.

In his dramatic appearance at Istanbul’s Atatürk Airport on Friday night, Erdoğan blamed the insurrection on the exiled cleric Fatullah Gulen, a reclusive figure who lives in the Poconos. “I have a message for Pennsylvania,’’ Erdoğan said, a reference that must have baffled many non-Turks. “You have engaged in enough treason against this nation. If you dare, come back to your country.”

Gulen, an aging cleric who heads one of the world’s largest Islamic orders, fled Turkey in 1999, when it appeared that the military was going to arrest him. For years, he was one of Erdoğan’s closest allies, helping him in his rise to power. While Gulen preaches a message of love and tolerance, there has often been something mysterious about him and his followers, who do not readily advertise either their affiliation or their intentions. Over the years, Gulen’s followers quietly found positions within many Turkish institutions, particularly the courts and police. (It was the Gulenists who led the show trials against the generals and the press.) In 2008, James Jeffrey, the American ambassador, wrote a memo about the Gulenist infiltration of the Turkish National Police. “The assertion that the T.N.P is controlled by the Gulenists is impossible to confirm, but we have found no one who disputes it,” Jeffrey said.

Then, in 2013, Gulen and Erdoğan split, in what appears to be part of a naked struggle for power. In the years since, Erdoğan has purged the courts and police of thousands of men and women presumed to be Gulen loyalists. It’s hard to know whether Gulen was behind Friday’s attempted putsch, but at this point it seems unlikely. While Gulen’s followers predominated in the security services, they were not generally believed to be a large force inside the military. It seems more likely that the officers who led the revolt represented the remnant of the military’s old secular order. Now they’re finished.

During his speech last night at the Istanbul airport, Erdoğan referred to the attempted coup as a “gift from God.” Erdoğan is usually a precise speaker, but in this case, perhaps in his excitement, he showed his cards. With the coup attempt thwarted, he will no doubt seize the moment. In recent months, Erdogan has made little secret of his desire to rewrite the constitution to give himself near total power. There will be no stopping him now.