}
 }
 
+static int remove_block_group_item(struct btrfs_trans_handle *trans,
+                                  struct btrfs_path *path,
+                                  struct btrfs_block_group *block_group)
+{
+       struct btrfs_fs_info *fs_info = trans->fs_info;
+       struct btrfs_root *root;
+       struct btrfs_key key;
+       int ret;
+
+       root = fs_info->extent_root;
+       key.objectid = block_group->start;
+       key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+       key.offset = block_group->length;
+
+       ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
+       if (ret > 0)
+               ret = -ENOENT;
+       if (ret < 0)
+               return ret;
+
+       ret = btrfs_del_item(trans, root, path);
+       return ret;
+}
+
 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
                             u64 group_start, struct extent_map *em)
 {
        struct btrfs_fs_info *fs_info = trans->fs_info;
-       struct btrfs_root *root = fs_info->extent_root;
        struct btrfs_path *path;
        struct btrfs_block_group *block_group;
        struct btrfs_free_cluster *cluster;
 
        spin_unlock(&block_group->space_info->lock);
 
-       key.objectid = block_group->start;
-       key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
-       key.offset = block_group->length;
-
        mutex_lock(&fs_info->chunk_mutex);
        spin_lock(&block_group->lock);
        block_group->removed = 1;
        /* Once for the block groups rbtree */
        btrfs_put_block_group(block_group);
 
-       ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
-       if (ret > 0)
-               ret = -EIO;
+       ret = remove_block_group_item(trans, path, block_group);
        if (ret < 0)
                goto out;
 
-       ret = btrfs_del_item(trans, root, path);
-       if (ret)
-               goto out;
-
        if (remove_em) {
                struct extent_map_tree *em_tree;