# Mautic database is huge, can I manually delete old records?

**URL:** <https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646>\
**Category:** Product Support\
**Created:** [February 9, 2022, 3:21pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646 "2022-02-09T15:21:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![instamaker](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/instamaker/32/481_2.png) [@instamaker](https://forum.mautic.org/u/instamaker)\
**Post date:** [February 9, 2022, 3:21pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/1 "2022-02-09T15:21:44Z")

</div>

**Your software**  
My Mautic version is: 3.2.0  
My PHP version is: 7.3.26  
My Database type and version is: MySQL v8

**Your problem**  
My Mautic database is pretty huge at the moment, it’s 56 GB total. My biggest tables are page\_hits (24 GB), email\_stats (19 GB), and campaign\_lead\_event\_log (6GB).

There are leads in my database that are from 3-4 years ago, that are not involved in any campaigns, and are essentially contacts I will never be adding into any future campaigns or be contacting through Mautic. This is typically the case with my entire database — once they are signed up, they are contacted over 3-4 weeks and then never contacted again. I’m thinking I can just delete older contacts.

To improve performance, I’m wondering if I can just do a standard ‘delete from’ query and delete all records that are older than 6 months old, from those two tables (page\_hits and email\_stats).

My questions are:

- Would that cause any consistency issues?
- Would deleting these records improve performance in any way?
- Is there anything I should be aware of? Or any alternatives to increase performance?

Btw, I’ve been using Mautic for 3-4 years now, and apart from being a bit slow when dealing with a large number of contacts, it’s been a total gamechanger in terms of saving me money. Thank you!

---

<div class="post-metadata">

**Author:** ![ekke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/ekke/32/296_2.png) [@ekke](https://forum.mautic.org/u/ekke)\
**Post date:** [February 9, 2022, 3:53pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/2 "2022-02-09T15:53:45Z")

</div>

Most importantly: make sure that for every lead the youngest entry in campaign\_lead\_event\_log does NOT get deleted!

---

<div class="post-metadata">

**Author:** ![instamaker](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/instamaker/32/481_2.png) [@instamaker](https://forum.mautic.org/u/instamaker)\
**Post date:** [February 10, 2022, 1:50pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/3 "2022-02-10T13:50:08Z")

</div>

So if I just delete records from page\_hits and email\_stats (the biggest two tables), but I leave the campaign\_lead\_event\_log table alone, I should be all good?

Thank you! 🙂

---

<div class="post-metadata">

**Author:** ![raramuridesign](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/raramuridesign/32/13226_2.png) [@raramuridesign](https://forum.mautic.org/u/raramuridesign)\
**Post date:** [February 10, 2022, 4:16pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/4 "2022-02-10T16:16:46Z")

</div>

@instamaker There is a cronjob which you can add which purges data older than X days.  
It might make sense if you run this command to keep the database at a decent level.

View the docs here, look for **Clean up old data**

> **[Cron jobs](https://docs.mautic.org/en/setup/cron-jobs)**
>
> . . . . Mautic 3 introduced a new path for cron jobs bin/console if you are using the legacy Mautic 2.x series you should replace this with the older version, app/console. . . . Mautic requires a few cron jobs to handle some maintenance tasks such as...

M

---

<div class="post-metadata">

**Author:** ![mneumann](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mneumann/32/2512_2.png) [@mneumann](https://forum.mautic.org/u/mneumann)\
**Post date:** [February 14, 2022, 10:57pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/5 "2022-02-14T22:57:53Z")

</div>

Yes the cron job helps a lot of cleaning out old data. If you want you can even clean out inactive leads. The page hit records you can delete without huge consequences, unless you really need this data. I deleted once the email stats, but repented afterwards, since I lost open rate statistics, indicators of inactive leads and a few other things.

---

<div class="post-metadata">

**Author:** ![sfahrenkrog1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/sfahrenkrog1/32/4583_2.png) [@sfahrenkrog1](https://forum.mautic.org/u/sfahrenkrog1)\
**Post date:** [February 15, 2022, 2:23pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/6 "2022-02-15T14:23:45Z")

</div>

Hey Instamaker

You could do this commands on the shell (backup + on your own risk!)

> <https://gist.github.com/sebastian-fahrenkrog/0d53ced6a50ed680383f4680c6398635>

We do them regularly but I could give you no garanty.

There is a new plugin which does some campaign\_lead\_event\_log cleanups:  
[https://github.com/Leuchtfeuer/mautic-housekeeping-bundle](https://github.com/Leuchtfeuer/mautic-housekeeping-bundle)

\*\*Update: \*\*  
**Sorry made a mistake in the audit log statement. Fixed it in the gist.**

Greetings  
Sebastian

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [February 15, 2022, 4:39pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/7 "2022-02-15T16:39:30Z")

</div>

I am at your debt master.

---

<div class="post-metadata">

**Author:** ![ekke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/ekke/32/296_2.png) [@ekke](https://forum.mautic.org/u/ekke)\
**Post date:** [February 15, 2022, 7:47pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/8 "2022-02-15T19:47:58Z")

</div>

> [@sfahrenkrog1](#):
>
> There is a new plugin which does some campaign\_lead\_event\_log cleanups:
> 
> [GitHub - Leuchtfeuer/mautic-housekeeping-bundle](https://github.com/Leuchtfeuer/mautic-housekeeping-bundle)

FYI we just set this repo to “private” for the time being, because it is a bit too aggressive.  
See my comment above:

> Most importantly: make sure that for every lead the youngest entry in campaign\_lead\_event\_log does NOT get deleted!

Read: The next version of the plugin will honor that and leave campaign status intact even for very old contacts. Please be patient 🙂

---

<div class="post-metadata">

**Author:** ![sfahrenkrog1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/sfahrenkrog1/32/4583_2.png) [@sfahrenkrog1](https://forum.mautic.org/u/sfahrenkrog1)\
**Post date:** [February 16, 2022, 9:27am UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/9 "2022-02-16T09:27:05Z")

</div>

Thanks for the hint ekke! I will try to add this to my sql query!

---

<div class="post-metadata">

**Author:** ![sfahrenkrog1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/sfahrenkrog1/32/4583_2.png) [@sfahrenkrog1](https://forum.mautic.org/u/sfahrenkrog1)\
**Post date:** [February 16, 2022, 10:41am UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/10 "2022-02-16T10:41:43Z")

</div>

Good Morning everybody

Regarding the comment from ekke:

> Most importantly: make sure that for every lead the youngest entry in campaign\_lead\_event\_log does NOT get deleted!

I tried to adapt my sql query

My existing query was like this:  
`DELETE FROM lead_event_log where bundle="lead" and object="import" AND date_added < (NOW() - INTERVAL 30 DAY);`

I try to build a query which selects for every lead\_id, campaign\_id combination the latest entry:

```auto
# select for every combination of lead_id and campaign id the last event log id 
select max(id),lead_id, campaign_id, count(id) FROM campaign_lead_event_log group by lead_id, campaign_id order by lead_id

```

(i added lead\_id, campaign\_id and the number of items for debugging reasons in the select statement)

If you combine this with my old query you would get something like this:

```auto
# select all entries with a triggered date oder than 30 days and which are not the last entry in the log per leadid and campaign 

select * FROM campaign_lead_event_log 
	WHERE date_triggered < (NOW() - INTERVAL 30 DAY) 
	and campaign_lead_event_log.id not in ( select max(id) FROM campaign_lead_event_log group by lead_id, campaign_id )

```

Of course this query needs now some debugging love.

@ekke What do you think? Is this somewhere near your own solution of the plugin you build?

Greetings  
Sebastian

---

<div class="post-metadata">

**Author:** ![instamaker](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/instamaker/32/481_2.png) [@instamaker](https://forum.mautic.org/u/instamaker)\
**Post date:** [March 7, 2022, 7:38am UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/11 "2022-03-07T07:38:51Z")

</div>

Looking forward to the update on the plugin too, thank you. Please let us know when you think you’ll have an update ready 🙂

---

<div class="post-metadata">

**Author:** ![pierre\_a](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/pierre_a/32/7031_2.png) [@pierre\_a](https://forum.mautic.org/u/pierre_a)\
**Post date:** [March 17, 2022, 6:07pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/12 "2022-03-17T18:07:46Z")

</div>

Hello @ekke, I’m very interesting by your plugin.

Pierre

---

<div class="post-metadata">

**Author:** ![techbill](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/techbill/32/8325_2.png) [@techbill](https://forum.mautic.org/u/techbill)\
**Post date:** [March 18, 2022, 3:43pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/13 "2022-03-18T15:43:00Z")

</div>

Looking forward to try your plugins … give us a post when it’s released!

---

<div class="post-metadata">

**Author:** ![instamaker](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/instamaker/32/481_2.png) [@instamaker](https://forum.mautic.org/u/instamaker)\
**Post date:** [July 28, 2022, 5:14pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/14 "2022-07-28T17:14:27Z")

</div>

Hi ekke — I hope you’re well. I wanted to check if you had an update on the mautic housekeeping bundle? Would you be able to keep the plugin open for use?

My mautic queries are massively hogging up the database and I was wondering if there was a way to clean up campaign\_lead\_event\_log. You mentioned that I should keep the youngest entry in campaign\_lead\_event\_log. Would you happen to know what the query might end up looking like?

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [July 30, 2022, 6:21am UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/15 "2022-07-30T06:21:48Z")

</div>

Hello,  
Here is an article about making your DB smaller documented with screenshots and commands.  
Good luck!

> **[The great Mautic weight control – Joey Keller](https://joeykeller.com/the-great-mautic-weight-control/)**

---

<div class="post-metadata">

**Author:** ![miamiman](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/miamiman/32/1118_2.png) [@miamiman](https://forum.mautic.org/u/miamiman)\
**Post date:** [January 2, 2023, 9:31pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/16 "2023-01-02T21:31:28Z")

</div>

Hello there. Was this plugin ever made public? I can’t seem to find it.

Thanks,

---

<div class="post-metadata">

**Author:** ![ekke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/ekke/32/296_2.png) [@ekke](https://forum.mautic.org/u/ekke)\
**Post date:** [January 3, 2023, 8:05am UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/17 "2023-01-03T08:05:19Z")

</div>

Hi there, pls send me your github handle and I can let you in 🙂

---

<div class="post-metadata">

**Author:** ![miamiman](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/miamiman/32/1118_2.png) [@miamiman](https://forum.mautic.org/u/miamiman)\
**Post date:** [January 3, 2023, 2:41pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/18 "2023-01-03T14:41:26Z")

</div>

> **[miamicityman - Overview](https://github.com/miamicityman)**
>
> miamicityman has 2 repositories available. Follow their code on GitHub.

Thanks,

---

<div class="post-metadata">

**Author:** ![techbill](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/techbill/32/8325_2.png) [@techbill](https://forum.mautic.org/u/techbill)\
**Post date:** [January 3, 2023, 2:55pm UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/19 "2023-01-03T14:55:36Z")

</div>

What it take to be invited to try the plugin?

---

<div class="post-metadata">

**Author:** ![ekke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/ekke/32/296_2.png) [@ekke](https://forum.mautic.org/u/ekke)\
**Post date:** [January 4, 2023, 7:54am UTC](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646/20 "2023-01-04T07:54:16Z")

</div>

2 more beta users to give 😉  
 → Do send me your Github handle, @techbill

[Next page](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646.md?page=2)
