How to lock/unlock a field in Drupal

Field instances provided by modules are usually locked so you can't delete them or modify structural information. An example of this is Drupal Commerce.

The field locking system in Drupal is quite poor and doesn't manage some situations where there's a legitimate need to change some field settings that won't never affect the stored data or the field's behaviour. For example you may want to unlock a field to change the field label or some widget settings.

Here's a code snippet to lock/unlock a field. It can be run quickly via devel's php block. Use it at your own risk.

$field_name = 'field_name';
$field = field_read_field($field_name);
$field['locked'] = 0; // 0: unlock; 1: lock.