Is there any way to delete field collection items programmatically in Drupal 8?

I have created a field collection item and attached it to the user entity in the following method.

$user_id = \Drupal::currentUser()->getAccount()->id();
$user = User::load($user_id);

$fc = FieldCollectionItem::create(array(
  "field_name" => "field_hobbies",

$fc->set('field_name', 'Watch TV');

It works fine and the field collection item got created successfully. Now I want to delete this field collection item. That means, I want to delete the hobby from the user. Is there any way for doing this?

1 Answer 1


Copied from (and then tested):


$user = User::load($user_id);
$storage = \Drupal::entityTypeManager()->getStorage('field_collection_item');
$fc_items = [];

foreach ($user->get('field_hobbies') as $item) {
    $fc_items[] = $item->value;

if (!empty($fc_items)) {
    $items = $storage->loadMultiple($fc_items);
    $user->set('field_hobbies', array());


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.