3

I have one section with different entry types. One entry type uses two matrix fields, another one only one, and so on.

Since the frontend layout is always the same, I use one template for the whole section:

{% extends "_layouts/main" %}

{% set title = entry.title %}

{% block content %}
    {% include "_fields/content__circles" %}
    {% include "_fields/content__body" %}
    {% include "_fields/content__links" %}
{% endblock %}

This works great, except when I change an entry to another entry type: Because the field-data isn't deleted from the db and shows up anyway, even though the field isn't added to the new entry type.

This is actually great, because that way your data isn't gone if you delete a field by mistake etc., but over time the db gets bloated, which I want to prevent.

Question: How can I delete data of fields which aren't used without editing each entry by hand or going in the db?

Clearification: entrytype 1 has content__circles and content__body, entrytype 2 has only content__body. After switching from type 1 to 2, the data of content__circles is still kept in the database, and is even accessible inside the entry-variable.

  • @DouglasMcDonald I'm not sure it's the same question. He's talking about swapping entry types, not field types. But I'm still confused what the OP is asking. Can you clarify some more, Victor? – Brad Bell Oct 10 '14 at 17:25
  • @BradBell I added edited the question, is it clearer now? – Victor In Oct 10 '14 at 20:18
  • I think so... if there is a common field between entry types, then they'll share the same data by design. All of that data (for every entry type a section has) is stored in the craft_content table, so I don't think there is "bloat" where you think there is, unless I'm missing something. – Brad Bell Oct 11 '14 at 4:21
  • @BradBell You're right brad. Sorry. Completely miss read that. I deleted my comments to avoid confusion. – Douglas McDonald Oct 11 '14 at 5:05
  • @BradBell But after switching the entry-type why is it still available in the entry-object? The field had content in entrytype1, but this field isn't available anymore in the entrytype2, so shouldn't the old field-data be excluded/deleted? – Victor In Oct 11 '14 at 13:29
6

You can get the fields that aren’t in use anywhere by running this SQL query:

SELECT f.*
FROM craft_fields f
LEFT JOIN craft_fieldlayoutfields l ON l.fieldId = f.id
WHERE l.id IS NULL

The easiest way to delete them would be from the Control Panel, but if you really want to limit your work to the database, you would need to delete those fields’ rows in craft_fields, and their corresponding column (“field_TheFieldHandle”) in craft_content. For Matrix fields, you would also need to drop the corresponding DB table (“craft_matrixcontent_TheFieldHandle”).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.