The gunman who killed 26 churchgoers in Texas escaped a mental health clinic in 2012, according to a police report.
El Paso officers who detained Devin Kelley were told he was “a danger to himself and others”.
Kelley had been sent to the hospital after he was court-martialled for assaulting his ex-wife and stepson during his stint in the US Air Force.
He was “attempting to carry out death threats” against “his military chain of command”, the report states.
Officials say the assault charge should have legally barred him from owning guns.
El Paso police arrested Kelley at a bus terminal in downtown El Paso on the evening of 7 June 2012, according to a police report first revealed by KPRC in Houston.
In the report, police wrote that Kelley had escaped from Peak Behavioral Health Services in Santa Teresa, New Mexico, about 100 miles (160km) away.
Media playback is unsupported on your device Media captionTexas shooting: How to spot a hoax
The person who reported him missing from the facility told police Kelley “suffered from mental disorders”.
Kelley “had already been caught sneaking firearms onto Holloman Air Force Base”, the report adds.
Later that year, Kelley pleaded guilty in a military court to repeatedly assaulting his wife and his toddler stepson.
He was sentenced to one year in a US Navy prison.
FBI investigators said on Tuesday they have been trying to unlock Kelley’s mobile phone, to better understand what led him to carry out the mass shooting.
Image copyright Getty Images
According to the Houston Chronicle newspaper, the killer had five days earlier shown up at the First Baptist Church of Sutherland Springs with his children for an annual fall festival.
A friend of 26-year-old Kelley’s former mother-in-law Michelle Shields, who officials believe he was targeting on Sunday, said she was glad to see him at the event with her grandchildren.
“They thought, ‘oh this is good. This is progress,'” said the woman, who is described as a longtime resident and family friend.
Photos of the event on the church’s Facebook page show children dressed in Halloween costumes and playing games.
Several of the victims are also shown in the series of more than 40 photos.
Media playback is unsupported on your device Media captionHow US mass shootings are getting worse
Survivors have been describing how Kelley went pew to pew in the church shooting crying children.
In an interview with San Antonio television station KSAT, Rosanne Solis described the terror among congregants as the massacre unfolded.
Dressed in tactical gear and armed with an assault rifle and 450 rounds of ammunition, the gunman began shooting into the tiny wooden building from outside.
Ms Solis said he stormed through the front of the church, shouting: “Everybody die!”
“Everybody was saying, ‘Be quiet! it’s him, it’s him!'” said Ms Solis, who was sitting near the entrance.
She added: “Everybody got down, crawling under wherever they could hide. He was shooting hard.”
Witnesses said the gunman walked up and down the aisles looking for survivors to shoot.
Ms Solis’ husband, Joaquin Ramirez, told how he made eye contact with Annabelle Pomeroy – the 14-year-old daughter of the church’s pastor.
She was crying for help, Mr Ramirez told KSAT.
He said he motioned with his finger for her to stay quiet. Annabelle was killed.
Media playback is unsupported on your device Media caption’Our kids play together,’ says a resident whose neighbours are feared dead
Mr Ramirez said the gunman also killed young children who were crying, shooting them at point-blank range.
He and his wife survived by playing dead, though she was shot in the arm and he was hit by shrapnel.
Another survivor, Farida Brown, 73, thought she was going to die, her son David Brown told KENS-TV.
“The shooter was making his rounds, and he ended up there and started shooting this lady multiple times,” Mr Brown told the station.
Media playback is unsupported on your device Media captionJohnnie Langendorff: “I’m just a guy who wanted to do the right thing”
“And the lady looked at my mom the whole time, and my mom was looking at her and telling her, ‘It’s OK, you’re going to go to heaven. You’re going to go to heaven.’
“And then she knew it was her turn to be shot, and so she just started praying that God would take her soul to heaven.”
But at that moment a neighbour Stephen Willeford entered the church and began shooting at Kelley.
As Kelley fled in his car, Mr Willeford flagged down a passing motorist, Johnnie Langendorff.
The two gave chase in Mr Langendorff’s pickup truck until Kelley’s vehicle crashed in a ditch.
The gunman was found dead of a self-inflicted gunshot wound to the head, say police.