+# must be called inside a state_dbh transaction with flock held
+sub __cleanup_state {
+ my ($self, $txn_id) = @_;
+ my $user_id = $self->{user_id} // die 'BUG: no {user_id}';
+ $self->{pop3d}->{-state_dbh}->prepare_cached(<<'')->execute($txn_id);
+DELETE FROM deletes WHERE txn_id = ? AND uid_dele = -1
+
+ my $sth = $self->{pop3d}->{-state_dbh}->prepare_cached(<<'', undef, 1);
+SELECT COUNT(*) FROM deletes WHERE user_id = ?
+
+ $sth->execute($user_id);
+ my $nr = $sth->fetchrow_array;
+ if ($nr == 0) {
+ $sth = $self->{pop3d}->{-state_dbh}->prepare_cached(<<'');
+DELETE FROM users WHERE user_id = ?
+
+ $sth->execute($user_id);
+ }
+ $nr;
+}
+