A man behind bars for nearly four years for a gang rape that did not happen has been cleared after his accuser admitted she lied to make her friends feel sorry for her.

William McCaffrey hugged his lawyer after the same New York judge who had sentenced him to 20 years in prison threw out the case and apologised on Thursday.

"I've been waiting for this for a long time," the softly spoken McCaffrey, 32, said outside a Manhattan court.

"I'm just glad it's over."

New DNA tests played a part, but his exoneration hinged largely on his accuser recanting - a rarity after rape convictions, the head of a national prosecutors' group said.