The man accused of opening fire inside a South Carolina church should not have been allowed to purchase the gun used in the rampage, FBI officials said Friday.

FBI Director James Comey told reporters that the background check system failed to stop Dylann Roof, 21, of Lexington, South Carolina, from purchasing a gun.

He said Roof admitted to drug possession during an arrest which would have barred him from gun purchase under FBI guidelines.

Comey explained the mistake as confusion with the paperwork of Roof's February arrest after Roof admitted to having illegal prescription drugs.

On April 11, Roof tried to purchase a gun in Columbia, South Carolina.

Two days later, an FBI examiner began a background check on Roof.

The FBI said there was confusion on what agency arrested Roof in February. The paperwork was incomplete and inaccurate.

The wrong agency was contacted, leaving the FBI unaware of roof's drug admission, which would have blocked him from buying a gun.

Because of the confusion, on April 16 Roof was able to buy a .45-caliber handgun to carry out the attack.

"This case rips all our hearts out," Comey said.

The information was released the same day as the Confederate flag was removed from state capitol grounds in Columbia.

The flag's removal was prompted by the rampage inside a historic Charleston church that left nine dead, including the church's pastor.