code: def clean_inasafe_fields(layer):
      fields = []
      if layer.keywords['layer_purpose'] == layer_purpose_exposure['key']:
          fields = get_fields(
              layer.keywords['layer_purpose'], layer.keywords['exposure'])
      elif layer.keywords['layer_purpose'] == layer_purpose_hazard['key']:
          fields = get_fields(
              layer.keywords['layer_purpose'], layer.keywords['hazard'])
      elif layer.keywords['layer_purpose'] == layer_purpose_aggregation['key']:
          fields = get_fields(
              layer.keywords['layer_purpose'])
      try:
          if layer.keywords['inasafe_fields'][displaced_field['key']]:
              fields.append(displaced_field)
      except KeyError:
          pass
      expected_fields = {field['key']: field['field_name'] for field in fields}
      new_keywords = {}
      for key, val in list(layer.keywords.get('inasafe_fields').items()):
          if key in expected_fields:
              if isinstance(val, str):
                  val = [val]
              sum_fields(layer, key, val)
              new_keywords[key] = expected_fields[key]
      layer.keywords['inasafe_fields'].update(new_keywords)
      to_remove = []
      for field in layer.fields().toList():
          if field.name() not in list(layer.keywords['inasafe_fields'].values()):
              to_remove.append(field.name())
      remove_fields(layer, to_remove)
      LOGGER.debug(
          'Fields which have been removed from %s : %s'
          % (layer.keywords['layer_purpose'], ' '.join(to_remove)))