Skip to content

Instantly share code, notes, and snippets.

@sebastian-fahrenkrog
Last active March 17, 2024 16:38
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save sebastian-fahrenkrog/0d53ced6a50ed680383f4680c6398635 to your computer and use it in GitHub Desktop.
Save sebastian-fahrenkrog/0d53ced6a50ed680383f4680c6398635 to your computer and use it in GitHub Desktop.
# normal mautic cleanup up commandos (deletes only unused ips and anonymous leads which are at least 3 weeks old)
# must be called multiple times
php bin/console mautic:unusedip:delete -n --limit=99999999999
php bin/console mautic:unusedip:delete -n --limit=99999999999
php bin/console mautic:maintenance:cleanup --days-old=21 -n
php bin/console mautic:maintenance:cleanup --days-old=21 -n
# after 30 days we clear infos in email stats about specific email details => email is stil in lead profile
php bin/console doctrine:query:sql "UPDATE email_stats SET tokens = NULL WHERE date_sent < (NOW() - INTERVAL 30 DAY);"
php bin/console doctrine:query:sql "UPDATE email_stats SET open_details = NULL WHERE date_sent < (NOW() - INTERVAL 30 DAY);"
php bin/console doctrine:query:sql "UPDATE email_stats SET last_opened = NULL WHERE date_sent < (NOW() - INTERVAL 30 DAY);"
# we delete email stats without existing lead
php bin/console doctrine:query:sql 'DELETE from email_stats where lead_id is null;'
# after 6 month we delete email stats emails
php bin/console doctrine:query:sql 'DELETE from email_stats where date_sent < (NOW() - INTERVAL 180 DAY);'
# after 3 month we only store emails stats which was ready by user
php bin/console doctrine:query:sql 'DELETE from email_stats where date_sent < (NOW() - INTERVAL 90 DAY) and is_read=0;'
# we clean audit log from no longer existent leads
sudo -u $LOCAL_USER $PHP_BIN $MAUTIC_DOCROOT/bin/console doctrine:query:sql 'DELETE al from audit_log al where bundle="lead" and object="lead" and object_id not in (select id from leads where id=al.object_id);'
# we clear notifications
php bin/console doctrine:query:sql "TRUNCATE TABLE notifications;"
# we delete all 1 month old import entries
php bin/console doctrine:query:sql 'DELETE FROM lead_event_log where bundle="lead" and object="import" AND date_added < (NOW() - INTERVAL 30 DAY);'
# we delete all pages hits no longer connected with any lead (anonymous or non anonymous)
php bin/console doctrine:query:sql 'DELETE from page_hits where lead_id is NULL;'
# This maybe risky todo / but works fine in most scenarios
# Old problem query: problem if the last campaing entry is removed
#php bin/console doctrine:query:sql "DELETE FROM campaign_lead_event_log WHERE date_triggered < (NOW() - INTERVAL 30 DAY);"
# New version:
# cleanup campaign_lead_event_log after 30 days
php bin/console doctrine:query:sql "DELETE clel FROM campaign_lead_event_log clel WHERE date_triggered < (NOW() - INTERVAL 30 DAY) and clel.id not in ( SELECT max_id FROM ( select max(id) as max_id FROM campaign_lead_event_log group by lead_id, campaign_id) AS tmptable );"
# cleanup campaign history in lead profile after 30 days
php bin/console doctrine:query:sql "DELETE FROM lead_event_log where bundle="campaign" and object="campaign" AND date_added < (NOW() - INTERVAL 30 DAY);"
# Maybe soft(er) solution (untested)
#php bin/console doctrine:query:sql "UPDATE campaign_lead_event_log SET metadata = '' WHERE date_triggered < (NOW() - INTERVAL 30 DAY);"
# Really important to free up mysql disk space
php bin/console doctrine:query:sql "OPTIMIZE LOCAL TABLE page_hits;"
php bin/console doctrine:query:sql "OPTIMIZE LOCAL TABLE audit_log;"
php bin/console doctrine:query:sql "OPTIMIZE LOCAL TABLE email_stats;"
php bin/console doctrine:query:sql "OPTIMIZE LOCAL TABLE notifications;"
php bin/console doctrine:query:sql "OPTIMIZE LOCAL TABLE campaign_lead_event_log;"
php bin/console doctrine:query:sql "OPTIMIZE LOCAL TABLE lead_event_log;"
php bin/console doctrine:query:sql "OPTIMIZE LOCAL TABLE ip_addresses;"
php bin/console doctrine:query:sql "OPTIMIZE LOCAL TABLE lead_ips_xref;"
@kukis2107
Copy link

Could you help me implement your script in two Mautic? Or do you only use it for yourself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment