# Migrate segments and contacts from M4 to M5, clean installation

**URL:** <https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554>\
**Category:** Mautic 5 Install/Upgrade Support\
**Created:** [December 28, 2024, 3:16pm UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554 "2024-12-28T15:16:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fkrafft](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/fkrafft/32/2220_2.png) [@fkrafft](https://forum.mautic.org/u/fkrafft)\
**Post date:** [December 28, 2024, 3:16pm UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554/1 "2024-12-28T15:16:18Z")

</div>

**Your software**  
My Mautic version is: M4  
My PHP version is: 7  
My Database type and version is: Maria

**Your problem**  
My problem is:  
I want to create a clean installation with the exception of importing the contacts and segments for these contacts.

Is there some convenient way of doing this in bulk instead of exporting each segment and then importing it?

FYI, I dont want to make a full upgrade.  
Instead I’ll do a clean M5 installation.

These errors are showing in the log:  
N/A

Steps I have tried to fix the problem:  
N/A

---

<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:** [December 30, 2024, 6:59am UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554/2 "2024-12-30T06:59:37Z")

</div>

The moist clean and convenient way is to export and import via Mautic UI.  
Anything else is less convenient.

---

<div class="post-metadata">

**Author:** ![fkrafft](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/fkrafft/32/2220_2.png) [@fkrafft](https://forum.mautic.org/u/fkrafft)\
**Post date:** [December 30, 2024, 7:17am UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554/3 "2024-12-30T07:17:32Z")

</div>

Assume you refer to the GUI and not an existing text command?

We are talking about more than 400 segments with different contacts. That’s a lot of manual clicking in the gui. Assume each export / import takes at least 2 min, equal to 800 min of work if it’s done manually for each segment.

Was thinking of writing a script to export each segment to a csv file and then have a script to import them again. Thoughts?

---

<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:** [December 30, 2024, 7:30am UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554/4 "2024-12-30T07:30:42Z")

</div>

Yes, you can do that as well.  
You have to make sure you

1. export ALL your contacts from Mautic mysql
2. import all your contacts into the new DB, make sure you import all contacts including contact ID!!!
3. export (via sql) segment information from the lead\_lists table, import into the new db
4. import the lead\_lists\_leads table via mysql. This is the table where segment membership is stored.

---

<div class="post-metadata">

**Author:** ![marcus42](https://avatars.discourse-cdn.com/v4/letter/m/ac91a4/32.png) [@marcus42](https://forum.mautic.org/u/marcus42)\
**Post date:** [January 1, 2025, 2:42pm UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554/5 "2025-01-01T14:42:28Z")

</div>

To answer your questions;

“Convenient”: That is quite subjective. Speaking for myself, it means 100% from a CLI

Copy the data from one installation to a second one;  
The API will be your friend. It’s possible to bulk export/import using the API.

It will avoid doing direct SQL tx and risk corrupting the DB.

Documentation about the API:  
[https://developer.mautic.org/#segments](https://developer.mautic.org/#segments)

How I would do it:

1. creating a loop to fetch all segments IDs
2. export all the subs for each segments using the IDs from first step
3. create the all segments on the new install (using the data fetch from the first step)
4. Add contacts to segments (fetched on the second step)

---

<div class="post-metadata">

**Author:** ![fkrafft](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/fkrafft/32/2220_2.png) [@fkrafft](https://forum.mautic.org/u/fkrafft)\
**Post date:** [January 7, 2025, 1:10am UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554/6 "2025-01-07T01:10:34Z")

</div>

I check both the API solution and the db solution.

**DB solution:**  
For the db it looks like the segments “lead list” and contacts are linked through the table “lead\_list\_leads”. See attached picture.  
So I would need to export these 3 tables, though the question is if the db structure for these 3 tables have changed from M4 to M5?

 ![Screenshot 2025-01-07 at 01.58.32](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/4/40204b28e4f03497330cbc7598f6f8b5dd3ee512.png)

**API solution:**  
The segment API doesn’t seem to have a option to list associated contacts.  
[https://developer.mautic.org/#segments](https://developer.mautic.org/#segments)

Instead I would have to use get segment membership for each contact.  
[https://developer.mautic.org/#get-segment-memberships](https://developer.mautic.org/#get-segment-memberships)

Would had been way more convenient if the API can list the contacts associated with each segment.

Will post what I end up doing.

---

<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:** [January 7, 2025, 12:11pm UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554/7 "2025-01-07T12:11:53Z")

</div>

As alternative you can just export the DB, set up a new Mautic 5, overwrite db with the freshly exported M4 db, and run migrations.  
Done.

---

<div class="post-metadata">

**Author:** ![fkrafft](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/fkrafft/32/2220_2.png) [@fkrafft](https://forum.mautic.org/u/fkrafft)\
**Post date:** [January 7, 2025, 6:02pm UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554/8 "2025-01-07T18:02:44Z")

</div>

Thanks, that would be neat but the old db is over 120GB.  
Wanna start over with a clean installation, only save the contacts and segments.

---

<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:** [January 7, 2025, 6:28pm UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554/9 "2025-01-07T18:28:40Z")

</div>

Delete your emails, audit logs, pageviews, notifications, and optimize DB.  
You’ll have a small DB.  
Now do the migration.

---

<div class="post-metadata">

**Author:** ![fkrafft](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/fkrafft/32/2220_2.png) [@fkrafft](https://forum.mautic.org/u/fkrafft)\
**Post date:** [January 17, 2025, 10:17pm UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554/10 "2025-01-17T22:17:18Z")

</div>

Done.

**How to clean up your mautic db:**  
Here is a summary for others of what I ended up doing.

Combining Joeyk’s solution, and Housekeeping plugin:

1. Check the size of the db and identify what table that need some cleaning  
_Credit:_ Joeyk:  
[The great Mautic weight control – Joey Keller](https://joeykeller.com/the-great-mautic-weight-control/)

sudo mysql;  
use mymauticdb;  
SELECT table\_schema AS “Database”, SUM(data\_length + index\_length) / 1024 / 1024 / 1024 AS “Size (GB)” FROM information\_schema.TABLES GROUP BY table\_schema;

SELECT table\_name AS “Table”,  
ROUND(((data\_length + index\_length) / 1024 / 1024), 2) AS “Size (MB)”  
FROM information\_schema.TABLES  
WHERE table\_schema = “mauticdb”  
ORDER BY (data\_length + index\_length) DESC;

1. Clean the select tables that needed cleaning:  
In my case the SQL query showed that “email\_stats” table was huge and needed some cleaning.  
Now we know what table to clean up and I prefer not to touch the db my self so I used the housekeeping plugin.  
_Credit: leuchtfeuer_  
[Tutorial - Housekeeping Plugin for Mautic](https://leuchtfeuer.com/en/mautic/know-how/mautic/tutorial-housekeeping-plugin-for-mautic/)

a. Download the .zip file from their github:

> **[GitHub - Leuchtfeuer/mautic-housekeeping-bundle](https://github.com/Leuchtfeuer/mautic-housekeeping-bundle)**
>
> Contribute to Leuchtfeuer/mautic-housekeeping-bundle development by creating an account on GitHub.

b. Transfer the .zip file to your mautic plugins folder …/docroot/plugins  
c. Unpack  
d. Rename the unzipped folder to "LeuchtfeuerHousekeepingBundle”  
e. Cache clear  
f. Reload plugins  
g. If necessary, activate the plugin from the webadmin tool in mautic.

1. Run Leuchtfeuer’s housekeeping plugin for the specific table you need cleaning.  
I needed to clean the “email\_stats”

```auto
bin/console leuchtfeuer:housekeeping -m -d [clean for days older than this]

```

-m is for email\_stats  
-d is for what days to clean up. It only removed data older than this value.

If you never cleaned the database, then the cleaning can take a long time.  
My tip is to start with a few days close to when you made the installation and test.

**NOTE:**  
Cleaning easily crash if you try to clean too many days in a row, recommend to use the (.sh) script found on the github page for housekeeping.  
It run one day at the time, avoiding the crash.

The script from the readme at the github also need slight modification to run.

1. Free up the space  
_Credit:_ Joeyk:  
[The great Mautic weight control – Joey Keller](https://joeykeller.com/the-great-mautic-weight-control/)

When Housekeeping plugin is done, your database is still taking up the previous space but it’s filled with empty information.  
To resolve this and free up diskspace by making the database smaller, run the sql query:  
“OPTIMIZE table email\_stats;”

Good luck!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/1X/43c63600fe51440378769136903f1fa2a9a34102.png) [@system](https://forum.mautic.org/u/system)\
**Post date:** [February 3, 2025, 4:28pm UTC](https://forum.mautic.org/t/migrate-segments-and-contacts-from-m4-to-m5-clean-installation/34554/11 "2025-02-03T16:28:05Z")

</div>

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.
