Q:

delete a specific element within field collection programmatically

This is driving me crazy it just sounds simple, but I cannot find the answer.
I have a field collection in a content type called field_collection_mine how you now while creating the node you can add elements to the field collection.
I have tried this:
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$wrapper->field_collection_mine = array();
entity_save('field_collection_item',$wrapper);

The code above delete all the elements within the field collection, but that is now what I want!! I just want to delete for example the last element. E.g:

Field Collection mine
Element1
Element2
Element3

How can I for example delete just the element 3? The above code deletes all the elements, so field_collection_mine is left empty.

A:

Finally I could achieve what I wanted. What I did was this:
In my case I wanted to delete the last item within the field collection.
Take a look to this code, for beginners: remember that you must have the $entity already loaded, e.g.
// Remove the field value
unset($entity->field_salida_mercanc_a[LANGUAGE_NONE][count($entity->field_salida_mercanc_a[LANGUAGE_NONE])-1]);

// Reset the array to zero-based sequential keys
$entity->field_salida_mercanc_a[LANGUAGE_NONE] = array_values($entity->field_salida_mercanc_a[LANGUAGE_NONE]);

// Save the entity
entity_save($entity_type, $entity);

Thats all! The other way is:
// Get the node wapper
$wrapper = entity_metadata_wrapper($entity_type, $entity);
// Get the last position of the array items, thanks to the ->value() statement you can get the complete Array properties.
$field_collection_item_value = $wrapper->field_collection_mine[count($wrapper->field_collection_mine->value())-1]->value();
// Do not use 'unset' to delete the item due to is not the correct way, I use the entity_delete_multiple provided by the entity API
entity_delete_multiple('field_collection_item', array($field_collection_item_value->item_id));

The way above is good using entity_metadata_wrapper function, but with that way there is a complex bug that I do not know how to solve it, you can check it at #1880312 and after apply the patch in #9 you get the next issue.

