Shaun Abraham was appointed by South Africa's former scandal-tainted president Jacob Zuma and faced accusations that he protected him from prosecution for corruption.
South Africa's Constitutional Court on Monday ruled the appointment of the chief prosecutor, who was given his job after former president Jacob Zuma removed his predecessor, was invalid and ordered that he be replaced within 90 days.
Shaun Abrahams was appealing against an earlier High Court judgment that ruled that the removal of predecessor Mxolisi Nxasana was unlawful and that his own appointment be revoked.
Zuma faced a string of corruption allegations during his time in office, and Abrahams was accused by the opposition and rights groups of shielding the president from prosecution.
Zuma has denied wrongdoing and Abrahams denies protecting him from prosecution.
Buying him out of office
"Zuma's removal of Nxasana was an abuse of power. Abrahams was a beneficiary of an abuse of power," Judge Mbuyiseli Madlanga, who criticised Zuma for buying Nxasana out of office with a $1 million (17 million rand) payout.
"The inference is inescapable that he was buying Mr Nxasana out of office."
Zuma, whose nine years in power were marked by economic stagnation and credit rating downgrades, resigned as president in February, reluctantly heeding orders from the ruling African National Congress.
The judge ordered that South Africa's President Cyril Ramaphosa appoint a National Director of Public Prosecutions within 90 days.
Abrahams, who had kept his post pending the appeal, was not in court. He had no immediate comment.
"He is disappointed but respects the decision of the highest court in the land," said the National Prosecuting Authority's (NPA) spokesman Luvuyo Mfaku said.
Calls to remove Abrahams
Several civil rights groups had asked the constitutional court to confirm to confirm the previous ruling, and remove Abrahams from office.
South Africa's opposition Democratic Alliance (DA) said Zuma's decision to appoint Abrahams as head of the NPA was nothing more than an attempt to prevent the prosecuting authority from reinstating corruption charges against him.
In March, Abrahams told a media conference that Zuma's attempts to head off the charges hanging over him for more than a decade had failed.
He announced Zuma would face 16 charges of fraud, racketeering and money laundering relating to a $2.5 billion arms deal to buy European military hardware to upgrade South Africa’s armed forces after the end of apartheid in 1994.
Zuma, who was ousted by the ruling party, denies any wrongdoing.