Files
leadchat/app/services/internal/remove_stale_contacts_service.rb
Vishnu Narayanan 0175714d65 feat: add job to remove stale contacts and contact_inboxes (#11186)
- Add a job to remove stale contacts and contact_inboxes across all accounts

Stale anonymous contact is defined as 
- have no identification (email, phone_number, and identifier are NULL)
- have no conversations
- are older than 30 days

---------

Co-authored-by: Pranav <pranav@chatwoot.com>
Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
2025-03-28 12:18:39 +05:30

20 lines
761 B
Ruby

class Internal::RemoveStaleContactsService
pattr_initialize [:account!]
def perform(batch_size = 1000)
contacts_to_remove = @account.contacts.stale_without_conversations(30.days.ago)
total_deleted = 0
Rails.logger.info "[Internal::RemoveStaleContactsService] Starting removal of stale contacts for account #{@account.id}"
contacts_to_remove.find_in_batches(batch_size: batch_size) do |batch|
contact_ids = batch.map(&:id)
ContactInbox.where(contact_id: contact_ids).delete_all
Contact.where(id: contact_ids).delete_all
total_deleted += batch.size
Rails.logger.info "[Internal::RemoveStaleContactsService] Deleted #{batch.size} contacts (#{total_deleted} total) for account #{@account.id}"
end
end
end