Delete companies that have no related contacts

Hello,

I wanted to clean up my Mautic contact database.
To do this, I want to delete the companies that have no related contacts.
In Mautic it is possible to do this manually company by company.

This is a bit time consuming. So I wrote an SQL query.
The results are : 15139 rows affected. (Query took 0.5347 seconds.)

DELETE FROM companies
WHERE id IN (
SELECT companies.id
FROM companies
EXCEPT 
SELECT companies_leads.company_id FROM companies_leads
)

If that helps.

If a developer goes through this and wants to make it a feature (e.g. via a CRON) that would be great. :slight_smile:

Pierre

1 Like

@pierre_a It has worked perfectly. Thank you so much .

One question @pierre_a : Would you know how to eliminate duplicate companies? Thank you

@ekke Maybe you want to add this to your cleanup script?

Thx, noted!