A man who spent more than three years in prison on a rape conviction has been freed after a family member found deleted Facebook messages that proved his innocence.

Danny Kay, 26, of Derby in England, had been jailed in 2013 after a woman accused him of rape following a sexual encounter the year before, according to local media. Key to his conviction were Facebook messages that appeared to show him apologizing for sex without the woman's consent.

It turned out the woman had selectively deleted messages in an apparent effort to prove her version of the story. It was only when Kay's sister-in-law Sarah Maddison found an archived version of the messages on his Facebook account that he was able to get the conviction overturned.

England's Court of Appeal in London ruled that police relied on an “edited and misleading” account of the Facebook conversation that was given to them by the complainant in the weeks after she claimed she was raped by Kay, the Daily Mail reported.

Kay told the paper that he owed his liberty to a conversation with a fellow inmate who convinced him the Facebook messages he thought were lost were recoverable.

Kay then asked Maddison to log in to his account. “I couldn't believe how easy it was to find the messages,” she told the Daily Mail. “I am no social media expert,” she said, but “it only took me a minute to find them, so how trained police couldn't is beyond me.” Kay had strenuously denied the charges to police.

The saga began in 2012 when Kay and the accuser had sex in what he described as a casual fling. He said it was consensual, she said it was not. At his trial, his accuser said there had been little contact between them after sex, the Independent newspaper reported. However, the full string of Facebook messages proved there was considerable contact and that the sex was consented to by both parties.

In particular, one edited message considered by the jury read “sorry,” implying that forced sex had taken place. The complete messages, however, showed that it was a response to the woman asking him why he was ignoring her. Also omitted from the version presented to the jury was her response: “Dnt [sic] be.”

Another factor in his conviction were edited messages that implied Kay had misled the woman about his age during their conversations, thus branding him as a liar. During their first Facebook conversation, the Daily Mail reported, he asked the girl how old she was, to which she replies: “Nearly 17.” (The age of consent in England is 16.) He then asked if she was single and she replied “yep.” He then said: “Same here.” However, in the messages shown to the jury, it appeared that his “Same here” response is a reference to his age.

In addition, the paper said, in one of the messages sent after the sexual encounter Kay asked the accuser for her phone number because he'd lost it. Not only did she supply it, but she also sent four kiss emojis. She later deleted this and other messages.

In another section of the deleted messages, the accuser says: “im still here for ya!” And in another communication, after the pair had split up, she said: “I thought u woulda at least tried to get me back.”

When the recovered messages were shown to the appeals court, the judges in a ruling in late December said that the exchange undermined the woman's account and supported Kay's version.

Derbyshire Police told the Daily Mail it would review its investigation “to find out whether lessons can be learned.”