4

During the installation procedure of a module one may create some fields and corresponding instances and attach them to a content type. What is the best practice to clear up what we have created after module uninstallation?

It seems that removing fields created by a module is not yet implemented in D7 (see field_modules_uninstalled docs.). What would be the best approach then to remove module fields?

OR

NOTE: the comment module removes the field upon uninstallation (see here).

  • 2
    field_delete_field() + field_purge_batch() is all you need; field_delete_instance() is called internally from field_data_field() anyway – Clive Jul 30 '13 at 9:07
  • Thanks for the hyperfast answer! I think you meant field_delete_field() and not field_data_field(), right? – user19626 Jul 30 '13 at 9:10
  • Yep I did, you must have seen it before I got the edit in :) – Clive Jul 30 '13 at 9:11
  • what would be then the best value for $batch_size argument of field_delete_field when removing let's say removing 3 fields? – user19626 Jul 30 '13 at 9:25
  • 2
    There's no 'best' value really, it completely depends on the size of data in the fields, the server setup, etc. I normally go for about 100 in custom code, but that figure isn't exactly scientific – Clive Jul 30 '13 at 9:45
2

This is the code snippet I usually use to remove fields created by a custom module:

if (field_info_field('my_field_name')) {
    field_delete_field('my_field_name');
    field_purge_batch(100);
}

Hope this helps

| improve this answer | |

Your Answer

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