* Top court ruled defendants' rights were violated
* Ruling may foster thaw in military-government ties
* Conflict in Syria, Iraq presents challenge to military (Adds reaction from military)
By Daren Butler
ISTANBUL, June 19 (Reuters) - A Turkish court ordered the release on Thursday of 230 military officers convicted of plotting to topple Prime Minister Tayyip Erdogan, a day after the country's top court ruled their trial was flawed.
The wife of one senior commander, held in prison for four years, described her husband's coming release as a "bittersweet happiness", and said the officers would "continue to fight for this country" after their release.
The Turkish military said it "shared the joy of the families of retired and active staff who have regained their freedom" and said it hoped a retrial would now lead to a just verdict.
The 2010-2012 "Sledgehammer" trial marked a high-point in Erdogan's drive to tame an army that for decades had dominated politics. Critics accused Erdogan at the time of using the courts to pursue a "witchhunt" against the generals.
In consigning hundreds of senior serving as well as retired officers to jail, the case eroded the authority and power of NATO's second biggest army while tension on borders with Syria and Iraq demanded increased commitments.
Defence lawyer Huseyin Ersoz said the officers were expected to be released within a few hours.
The constitutional court ruled unanimously on Wednesday that the officers' rights had been violated in the handling of digital evidence and the refusal to hear testimony from two former top military commanders as requested by defendants.
"Obviously I'am happy my husband is being released but it is a bittersweet happiness," Nilgul Dogan, wife of chief suspect, former First Army commander General Cetin Dogan, told Reuters. "These people were held unjustly in jail for four years due to a fabricated, wrongful, injust trial.
"They must give account to us for those four years. After our husbands have rested for some time, we will continue to fight for this country for our children and grandchildren."
Erdogan, his primacy over the army established, said early this year he was open to the idea of a retrial. Officials had suggested evidence had been manipulated by an influential Islamic cleric who had been using influence in police and judiciary to help Erdogan break the army's power.
Cleric Fethullah Gulen, a former ally of Erdogan turned bitter rival, denies any involvement in Sledgehammer and in a corruption investigation against Erdogan associates that the prime minister accuses him of concocting to topple him.
Release of the officers was not generally expected to undermine Erdogan's popularity and could even, at this stage, help ease relations with an officer corps he has excluded from policy-making bodies since coming to power in 2003.
The generals, who had removed four governments in four decades, viewed Erdogan with suspicion because of his Islamist past; but his popularity - he has won three elections in a row - afforded him protection while he moved to break their power.
ERDOGAN FOCUSED ON ELECTION
In March, a court ordered the release of a former military chief and other defendants accused of the separate "Ergenekon" plot also to topple the government.
Erdogan, who is expected to seek the presidency in an August election, is now focused on battling U.S.-based cleric Gulen, whom he also accuses of trying to unseat him.
The election comes at a time of heightened tensions on Turkey's frontiers. The armed forces have deployed additional defences on the Syrian border to cope with spillover from civil war there and a Sunni insurgency in Iraq has also raised alarm.
More than 300 officers were sentenced in September 2012 over the alleged "Sledgehammer" conspiracy and the appeals court upheld their convictions last October.
The alleged plot dates back to 2003, months after Erdogan first came to power, and was said to include plans to bomb mosques and trigger a conflict with Greece by shooting down one of Turkey's own warplanes to trigger a military takeover.
Turkey's armed forces were long the guardians of the secular republic established by Mustafa Kemal Ataturk, carrying out three coups between 1960 and 1980 and pushing an Islamist-led government from power in 1997.
Since first coming to power, Erdogan's Islamist-rooted AK Party has reined in army influence with a series of reforms designed to strengthen democracy, while prosecutors have pursued suspected coup-plotters in the army through the courts.
The leading defendants in the Sledgehammer case were Cetin Dogan, a former commander of the prestigious First Army, former air force commander Ibrahim Firtina and retired admiral Ozden Ornek, who were given 20-year prison sentences.
Sledgehammer and other trials sparked accusations that the government was using courts to silence political opponents.
Former army chief Kenan Evren, 96, was sentenced to life in jail on Wednesday for leading a 1980 coup that resulted in widespread torture, arrests and deaths. (Writing by Daren Butler; Editing by Nick Tattersall and Ralph Boulton)