I think the answer starts further back.
Some background: The modern Turkish state was founded by Kemal Ataturk out of the collapse of the Ottoman empire at the end of the First World War. His vision was that the country needed to have a strong Turkish identity distinct from its Islamic identity to create a sense of Turkish nationalism that differentiated it from its Muslim neighbours. So the Turkish state was founded as a secular state, and one of the roles of the military in that state was, as well as defending against external enemies, to defend the secular constitution. To that end, the military was considerably more autonomous than in many other countries and had a fair degree of independence from the government, precisely because part of its role was to defend the secular constitution from government encroachment, and to make sure that religion remained a matter of individual observence rather than state policy. (And the military intervened to remove governments they perceived as problematic several times in the 20th century).
Skip ahead to around 2000, and Turkey was making some reforms to try and support its bid to join the EU. The EU wants to be a club of democratic countries, and saw the repeated military 'coups' in Turkey to be a problem (rather than the military defending the secular democractic constitution). The EU demanded reforms to bring the miliary more under civilian control. The Turkish military went along with this because they supported the goal of Turkey joining the EU.
This gave the government increased control over the military in terms of finances and greated ability to remove officers for various reasons, with the net effect of increasing the proportion of officers with greater loyalty to the current government.
Some excepts from The Turkish “model” of
civil–military relations by Ozan O. Varol serve to underline this.
[T]he European Union officially designated Turkey as a candidate country
in 1999. Among the accession requirements to the European Union was effective civilian control of the military, a major obstacle to Turkey’s integration into
the EU. To advance Turkey’s candidacy to the EU, the Parliament adopted a
number of constitutional amendments in 2001 that curbed the military’s role in
the National Security Council. Among other things, the amendments increased
the number of civilians on the Council, emphasized the Council’s advisory role,
deprived the Council of its executive powers (e.g., the authority to request reports
from government agencies), and replaced the Council’s military Secretary-
General with a civilian leader. Pursuant to the amendments, the government
would no longer give priority consideration to the Council’s views and would
merely assess its recommendations. In addition, the military judges serving
on the state security courts (Devlet Guvenlik Mahkemeleri)—which had jurisdiction over cases involving crimes against state security—were removed and the
courts became entirely civilianized. The armed forces were also brought under
the jurisdiction of the Turkish Court of Accounts (Sayistay), which is authorized
to audit state departments. Turkey’s desire to become a member of the EU
thus provided a major impetus for the subordination of the military to civilian
leaders.
[I]n stark contrast to the weak coalition governments of the past, effect
ive governments took ahold of power beginning in 1999. The coalition government of Bulent Ecevit, Devlet Bahceli, and Mesut Yilmaz in 1999 successfully
adopted structural economic reforms and initiated a liberalization process with
the goal of full membership in the European Union. But the major shift came
with the ascension to power in 2002 of the Justice and Development Party (Adalet
ve Kalkinma Partisi) (AKP)
[...]
Armed with a fairly strong popular mandate, AKP sought to rein in the influence
of the military in Turkish politics. In addition to implementing additional measures
intended to bring the military within civilian control as part of the EU accession criteria, under AKP’s reign, the military also became a target of criminal investigations
and charges. Under the Ergenekon and Sledgehammer prosecutions—which allege
a conspiracy to overthrow the AKP government by a coup d’état—scores of gener-
als serving in the Turkish military have been imprisoned. Hundreds of retired military officers, including those that led the coups in 1980 and 1997, have also been
detained. The clash between the AKP government and the military culminated in the mass resignation of Turkey’s military high command in late July 2011.
So in summary, Turkey made reforms with the implicit consent of the military that brought the military under greater civilian control and reduced its influence, with the aim of joining the European Union. This increased civilian control put in place measures that Erodgan used to gain even greater control over the military.