How to prevent Migrate from creating null fields? I'm writing some migrations classes using Migrate module. Some of my source fields does not have data for migration but the Migrate module creates Drupal fields with null values anyway. How to prevent that? I only want to create fields if there are non-null values for them.

This comment suggests that it should be enough to just unset field from source row (for example inside prepareRow), but that doesn't work for me - the field with null value is created anyway.
Apparently this feature was added to Migrate 2.6 and for now is available only when you're using dev version. For those who want to try it out, there is RC1 version available.

For older Migrate module versions, the empty field needs to be unset in prepare function, not inside prepareRow as the comment from drupal docs page suggested. Here is an example:

function prepare($entity, $row) {
  // Assuming that 'year' maps to 'field_year'
  if (!isset($row-&gt;year) || $row-&gt;year === null)
    unset($entity-&gt;field_year);

  // Assuming that 'gender' maps to 'field_gender'
  if (!isset($row-&gt;gender) || $row-&gt;gender === null)
    unset($entity-&gt;field_gender);
}


There is also a more "generic" solution prosed in one of the comments on drupal.org (pasting it here for future reference):

/**
 * Removes empty values prior to migrating.
 * @see http://drupal.org/node/1665332
 * @see http://drupal.org/node/1792894
 */
function mymodule_remove_empties($destination, $entity) {
  if ($destination instanceof MigrateDestinationEntity) {
    $entity_type = $destination-&gt;getEntityType();
    $form = $form_state = array();
    _field_invoke_default('submit', $entity_type, $entity, $form, $form_state);
  }
}


And in each of your Migrate classes:

public function prepare($entity, stdClass $source_row) {
  mymodule_remove_empties($this-&gt;destination, $entity);
}