# 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:** 1\
**Showing post:** 10

<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

---

_[View the full topic](https://forum.mautic.org/t/mautic-database-is-huge-can-i-manually-delete-old-records/22646)._
