ATTENTION: These are MySQL database queries. In order to run these commands you must have access to your Mautic database. (most commonly using phpMyAdmin within your web host.) This cannot be done through your Mautic dashboard.
Usually your database manager will have a place to paste in your SQL command. Then you hit the button, and it returns the result.
These queries run on two standard database tables in Mautic:
lead_tags — This is the table with all the tags in the system
lead_tags_xref — A “cross reference” table that connects a contact with a tag. By counting the number of these references we can see how popular the tag is.
Note: Your installation might be using a prefix on the table names. In this case lead_tags would become yourname_lead_tags. If you get a “no such table” error, you will need to add your prefix into all the queries.
Get All Tags with a Usage Count
This query will show you all your tags, and a count of the number of contacts having this tag.
This will return a list of all tags (with the numerical id, and the tag name) plus a count column containing the number of contacts using this tag. It is sorted by count descending, meaning the most popular tags will be at the top.
Get Tags That Are Not Being Used
This is basically the same query as the one above, but it selects only those rows where the count is zero.
Delete Unused Tags
DANGER! This will delete tags that are not currently attached to any contact. It does not check for tags that are used in Segments or Campaigns.
I don’t think it should be an issue, but Mautic may run into trouble trying to look for a tag that does not exist in the database. User beware!
This uses a sub-query similar to the one above to locate all tags with zero uses, and issues the command to delete all the records it found.
If you want to try a test run, change DELETE FROM to SELECT * FROM and it will show you the records rather than deleting them.
Thanks to Yannick Pereira-Reis and this article for help with this query.
Delete A Tag In Use
You may wish to delete a tag from your database that is attached to your contacts. Perhaps it’s no longer helpful, and you want to tidy up. If you try just deleting a row from your lead_tags table you may get a “Foreign Key Constraint” error.
This is the structure of the database reinforcing the link between lead_tags and lead_tags_xref. The former cannot be removed while the latter relies on it.
Before you can remove a tag, you must first remove any contact links referencing it.
This is actually two sets of two queries each. The first set is if you know the ID number of the tag you want to delete. The second set looks up the ID based on a tag name.
In both case, the first query will delete any connections in the lead_tags_xref table first, then then second query deletes the tag itself from lead_tags.
I look forward to a Tag Manager becoming a part of Mautic, but in the meantime these queries can help you get some simple tasks done.