+sub _unref_doc ($$$$$;$) {
+ my ($sync, $docid, $ibx, $xnum, $oidbin, $eml) = @_;
+ my $s = 'DELETE FROM xref3 WHERE ibx_id = ? AND oidbin = ?';
+ $s .= ' AND xnum = ?' if defined($xnum);
+ my $del = $sync->{self}->{oidx}->dbh->prepare_cached($s);
+ $del->bind_param(1, $ibx->{-ibx_id});
+ $del->bind_param(2, $oidbin, SQL_BLOB);
+ $del->bind_param(3, $xnum) if defined($xnum);
+ $del->execute;
+ my $xr3 = $sync->{self}->{oidx}->get_xref3($docid, 1);
+ my $idx = $sync->{self}->idx_shard($docid);
+ if (scalar(@$xr3) == 0) { # all gone
+ $sync->{self}->{oidx}->delete_by_num($docid);
+ $sync->{self}->{oidx}->eidxq_del($docid);
+ $idx->ipc_do('xdb_remove', $docid);
+ } else { # enqueue for reindex of remaining messages
+ my $ekey = $ibx->{-gc_eidx_key} // $ibx->eidx_key;
+ $idx->ipc_do('remove_eidx_info', $docid, $ekey, $eml);
+ $sync->{self}->{oidx}->eidxq_add($docid); # yes, add
+ }
+ @$xr3
+}
+