Charleston, S.C., shooting suspect Dylann Roof is escorted from the Shelby Police Department in Shelby, N.C., on June 18. FBI Director James B. Comey says Roof should not have been allowed to purchase the gun used in the attack. (Chuck Burton/AP)

Dylann Roof, who is accused of killing nine people at a church in South Carolina three weeks ago, was able to purchase the gun used in the attack only because of lapses in the FBI’s background-check system, FBI Director James B. Comey said Friday.

Comey said Roof should have been prevented from buying the .45-caliber weapon used in the shooting, which authorities have said was motivated by Roof’s racist views. The political repercussions of the June 17 massacre at Emanuel African Methodist Episcopal Church in Charleston led South Carolina to remove the Confederate flag from its statehouse grounds Friday.

“This case rips all of our hearts out,” Comey said. “But the thought that an error on our part is connected to this guy’s purchase of a gun that he used to slaughter these good people is very painful to us.”

The breakdown was a result of errors not only by the FBI but also apparently by local law enforcement, and Comey said he has ordered a 30-day review to examine the procedures that led to the failure and to see if the process can be tightened. The errors came to light as investigators examined a gun purchase Roof made two months before the shooting.

Roof had been arrested for possession of narcotics in February, a charge that alone did not disqualify him from buying a gun. But Comey said Roof’s subsequent admission of the drug crime would have triggered an automatic rejection of his gun purchase if the information had been properly recorded in criminal-record and background-check databases.

Instead, Comey said, the data was not properly entered in databases of criminal records that are submitted by state, local and federal law enforcement agencies, and in records kept by the bureau. As a result, an FBI examiner assigned to review Roof’s purchase never saw his admission to the narcotics charge. The examiner runs checks under the National Instant Criminal Background Check System (NICS), which relies on these databases and the bureau’s own records.

Comey’s disclosure to reporters who were summoned to the bureau’s headquarters Friday amounted to a heartbreaking admission by the FBI director that the attack on a Bible study group might have been averted.

The failure to block Roof’s gun purchase is likely to renew scrutiny of a federal background-check system that also allowed troubled individuals to acquire firearms before previous shootings.

On Capitol Hill, Senate Judiciary Committee Chairman Charles E. Grassley (R-Iowa) promptly sought to head off any effort to tighten gun-control laws. “It’s disastrous that this bureaucratic mistake prevented existing laws from working and blocking an illegal gun sale,” he said in a statement. “The facts undercut attempts to use the tragedy to enact unnecessary gun laws. The American people, and especially the victims’ families, deserve better.”

The Center for American Progress, meanwhile, used the case to call for tightening the law on background checks. “Like the Virginia Tech massacre, the Columbine massacre, and countless every day shootings, gaps in our gun background check system contributed to the Charleston attack,” said a statement from Arkadi Gerney, senior vice president at the center. “The answer is simple: all records of prohibited individuals need to go in the FBI system and every gun sale needs to go through a background check.”

Gabrielle Giffords, the former Arizona congresswoman who was seriously wounded in a gun attack in 2011, and her husband, Mark Kelly, said they were “deeply troubled to learn details of the failures” and called for a full investigation.

Comey said Roof’s transaction began when he went to a gun store in West Columbia, S.C., on April 11. The dealer submitted his biographical information to the NICS, which handles background checks for gun purchases in about 30 states, including South Carolina.

Dylann Roof is in custody after police say he opened fire at a historic African American church in Charleston, S.C. Here’s a look at the 21-year-old's background, including recent arrests, and what authorities say happened inside the church. (Alice Li/The Washington Post)

Under the law, the FBI has three business days to deny or approve a purchase. If a decision is not made in that time frame, the law permits the dealer to complete the sale.

On April 13, a veteran FBI examiner who routinely handled 15 or more cases a day pulled up Roof’s request. Checking his criminal record, she saw a record of the narcotics arrest, which erroneously listed the arresting agency as the Lexington County Sheriff’s Department. She faxed a request for information to the sheriff’s office and to the county prosecutor. But officials at the sheriff’s office told her they were not handling the case and referred her to the Columbia Police Department.

The fact that the Columbia police handled the arrest — not the sheriff’s department — was not reported in the NCIC, which relies on the agency in question to submit information.

“It’s not clear why that happened,” Comey said of the error, “but it made a big difference.”

The sheriff’s department, which unlike the Columbia Police Department has a jail, booked Roof, and that might have led to the confusion.

Had the examiner been able to see the Columbia police report that Roof admitted to possession of a drug, or had the county prosecutor gotten back to her and told her of the report’s existence, “that transaction would have been denied,” Comey said. The prosecutor’s office never replied to her query.

The examiner’s effort was further tripped up by a geographic irregularity that the FBI did not reflect in its system.

Only a small part of the city of Columbia is located in Lexington County, with most in neighboring Richland County. The FBI’s system did not account for that jurisdictional split, and the examiner contacted only the West Columbia Police Department, which reported no record of Roof’s arrest.

On April 16, after the mandatory three-day waiting period lapsed with no adverse ruling from the FBI, Roof got his gun — a .45-caliber Glock pistol. There is no requirement for a dealer to notify the bureau when it has sold a gun, bureau officials said.

“There were a combination of things; one could say highly improbable events came together in this circumstance,” Comey said. “But the one we are definitely accountable for is not having reflected the geography of South Carolina on our contact sheet.”

Comey said he had spoken to the examiner and described her as “heartbroken,” but he said she had followed proper procedures.

FBI agents in South Carolina met Friday with the families of the victims to explain what happened, he said.

This report has been updated to correct the kind of drug charge Roof was facing in South Carolina. It was a misdemeanor not a felony charge.

Greg Miller, Sari Horwitz and Mike DeBonis contributed to this report.