A legal and ethical drama has come to an end over whether Robby the chimpanzee should live out his years among people or fellow apes.
Robby, who is in his forties and born in a zoo, has spent most of his life with German circus ringmaster Klaus Köhler and his family.
Animal rights groups have for years called for his rehabilitation among other apes in a specialist sanctuary.
But a court's 2015 decision to move him from the circus has been overturned.
Klaus Köhler of Circus Belly appealed to a higher court in the northern state of Lower Saxony, arguing that that any move would be tantamount to a death sentence for Robby.
The court in Lüneburg agreed that handing him over was unreasonable at such a late stage in his life.
Chimpanzees live up to 40 years in the wild and into their fifties in captivity.
Robby is thought to be the last chimp in a German circus and his age is unclear. Mr Köhler believes he is 47, but the court said he was 43.
"I'm glad I can keep my child," the circus director said. "Robby is my seventh child and belongs to the family."
Animal rights groups were disappointed with the ruling and repeated their call for a nationwide ban on wild animals in circuses.
A German court ruled that a legendary 47-year-old circus chimpanzee should spend the rest of his days among people, rather than with other apes.https://t.co/k6BudFsbxw— DW News (@dwnews) November 9, 2018
Peta launched its "Save Robby" campaign in 2011, posting pictures of a dejected-looking chimp in what it called "horrific conditions" in a circus wagon.
"The animal has had a sad life and never had adequate social partners," it argued.
Animal expert Pierre Grothmann, a key witness in the case, told the court that owing to his upbringing the chimpanzee lacked social skills as well as contact with others and had developmental disorders.
However, Mr Köhler, 70, insisted his family spent four hours a day with the chimp and described Robby as a "personality with human features".
The court decided that the earlier ruling had failed to take into account the effects of a relocation so late in Robby's life.
Although Robby was badly disturbed, they assessed that staying with the circus was the lesser of two evils.