Is it ever okay to excommunicate believers from the church?
Old Testament Example
Ezra, in chapter 10, mourned his fellow believers because they were living immoral lives. These people saw his mourning and made a proposal, which he accepted: the believers would turn from their sin and return to following Godly living (Ezra 10:3). If after three days they did not repent, they would be “excluded from the assembly of exiles,” (Ezra 10:8).
New Testament Example
After Jesus heals a blind beggar, the dissenting Pharisees began discussing who the man believed Jesus was. His reasoning went from calling Jesus a man (John 9:11), to a prophet (v. 17) who might have disciples (v. 27), to one "from God" (v. 33), to agreeing with Jesus that He is the Son of Man and to be properly worshiped (v. 38). The result was that the Pharisees excommunicated the man from their synagogue (John 9:22).
On several occasions people within the church were called out for the lives they were living and told - commanded even - to be removed from close fellowship from other believers. Paul explains that because believers are “new leaven” in Christ, we should be characterized by sincerity and truth (1 Cor 5:6-8). Allowing fellow believers to confess Christ yet live immorally would create dissention and hindrances within the church (Romans 16:17-18).
Our charge, however, is not to make them our enemies as the Pharisees did in John 9, but to exhort them to repentance (2 Thes 3:14-15). We are called to exercise spiritual discipline over the body of believers (1 Cor 5:12) for the purpose of accountability, so that Christ’s name is glorified and not dishonored. The narrow line here is not to judge, because that is ultimately God’s work, but to appeal to others so they return to repentance and right relationship with God (and thus with His people).