"It would not be right for me to pick and choose where, when and in what format I play for the Proteas. For me, in the green and gold, it must be everything or nothing," he added.
The 34-year-old, whose swashbuckling batting style and razor sharp fielding has made him one of the sport's leading lights, only returned to test cricket in December after a lengthy hiatus where he focused on the limited overs formats of the game.
"It's not about earning more somewhere else, it's about running out of gas and feeling that it is the right time to move on," he said. "Everything comes to an end. To the cricket fans around South Africa and the world, thank you for your kindness and generosity, and today, for your understanding.
"I have no plans to play overseas, in fact, I hope I can continue to be available for the Titans in domestic cricket. I will continue to be the biggest supporter of (captain) Faf du Plessis and the Proteas."
With a young family, De Villiers has hinted at international retirement in the last few years, but this past season committed to playing for his country in all three formats.
In his 2016 autobiography, he revealed he had two major career goals remaining -- to beat Australia in a home test series and win the Cricket World Cup.
He achieved the first of those earlier this year, but said last month he was no longer as determined to complete the second in England next year.
"I'm taking it one game at a time," De Villiers was quoted as saying by the International Cricket Council website.
"My ultimate dream is not to win a World Cup. I've changed my mindset. I feel it will be nice to win it, it'll be a bonus, but if I don't, it's not going to define my career."
De Villiers retires with a test average of 50.66 and as the fourth-highest scorer for South Africa with 8,765 runs. He made 22 test centuries and also took 222 catches and five stumpings, having spent part of his career in the role of wicketkeeper.
In ODI cricket, he holds the records for the fastest 50 (16 balls), 100 (31 balls) and 150 (64 balls).
"AB is one of the all-time greats of South African cricket who has thrilled spectators around the world with his sheer brilliance, coupled to his ability to innovate and take modern day batting in all three formats but particularly in the white ball ones to new levels," said Cricket South Africa president Chris Nenzani.
(Additional reporting by Nick Said; Editing by Christian Radnedge) ((email@example.com; +27 82 8257807; Reuters Messaging: firstname.lastname@example.org))