5

I have upgraded a website to drupal 8 that I found a field created and I need to delete that field as it became useless.

The field is locked, and this prevents me delete it from the interface. I tried to delete it using field_purge_field(), but it didn't work either.

The answer for How to properly delete fields, programmatically? didn't work for me.

Edit: I tried that piece of code but didn't do any thing

$field = \Drupal::entityManager()->getStorage('field_config')->load($entity_type.$bundle_name.$field_machine_name);
field_purge_field($field);

How do I delete fields that result locked? How do I unlock them before deleting them?

0

2 Answers 2

8

Purging is the second step, after the field was deleted. field.module should do that automatically for you.

A field config is an entity, you delete it like any other entity:

$field->delete();
1
  • ^^ yes, let's preserve tedious adherence to the letter of the rules instead of getting it right, especially when the tediousness gets it wrong. The edit which was proposed is what is needed to accomplish full deletion: $field = \Drupal::entityManager()->getStorage('field_config')->load($entity_type.$bundle_name.$field_machine_name); $field->delete(); field_purge_field($field);
    – 1kenthomas
    Commented Aug 26, 2016 at 5:26
1

Here is this answer, more plainly, tested and working:

\Drupal::entityManager()->getStorage('field_config')->load('my_entity_type.my_bundle.my_field_name');

But remember, the best way to do this, if you can, is simply to delete the field through the UI and then export your configuration using drush cex

1
  • \Drupal::entityTypeManager() instead of \Drupal::entityManager()
    – Heebinho
    Commented Apr 12 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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