Turkey has seen a rapid political change in the last decade, it has transitioned from a system where secularism was constitutionally mandated in which the current government would not have been allowed to govern. The military used to have the right to intervene in the political system. In 1997, the military removed the Turkish government from power, but because this happened within the constitutional framework, it doesn't qualify as a coup.
Now, when a country moves away from such a system to a different system where the military no longer has this power, it takes time for all the people working in the relevant institutions to change their mindset. There will still be many old guard people in the military and the judiciary who have difficulties accepting the new system. In case of this coup, if it had been successful, the military would have had to rely heavily on the old guard judges to get their way.
Now a reform process was underway to deal with these and other issues, but Erdogan also tried to get his way by giving the justice minster more power, which was ruled to be unconstitutional. So, there was a power play going on between the judiciary and the government, and because you have this "old guard judiciary" issue mixed in there, the Erdogan government does not fully trust the judiciary. So, it's understandable that that after this coup attempt, Erdogan would immediately get rid of the judges and prosecutors who are seen to be on the wrong side, who given their record would likely not have obstructed the coup plotters had they been successful.