0

I have one senario where 2 for content type i have added field collection field.

Each content type has around 3k plus nodes data. each nodes has 50+ records of field collections. All content creation done through excel import functionality.

For second content type, the field collection records that i imported is wrong. i want delete all the fields collection items only for second content type.

How to do it? i can't manually since it has 3k+ nodes and 50+ records in each nodes.

Any module which handle these kind of scenario? or any coding documentation which help me here.

as i search i got few related links as below. but none of them completely fit my requirement.

Could anyone please let me know or help me achieve this.

Deleting field collection items : As mentioned on this page i can't play with DB.

Field Collection Value field is not deleting correctly(using programmatically) : looks like i can use it deletion of data. but how for particular content type.

And I have to give one interface on the site where a non-technical guy can handle this if the same mistake happens.

If anybody knows please help.

Thanks in Advance.

1

My suggestion is to do this using code. IF your using D7, try following code in any custom module or in a basic page.

//Use a query to get all nodes of your particular content type.

$results = db_query("SELECT nid FROM node WHERE type = 'your-content-type'");
$nids = array();
foreach($results as $result){
  $nids[] = $result->nid;
}

foreach($nids as $nid){ // use for loop for testing
  $node = node_load($nid);
  foreach ($node->field_international_activity[LANGUAGE_NONE] as $key => $value) {
   // Build array of field collection values.
   $field_collection_item_values[] = $value['value'];

   // Unset them.  
   unset($node->field_international_activity[LANGUAGE_NONE][$key]);
  }

// Delete field collection items.
entity_delete_multiple('field_collection_item', $field_collection_item_values);
node_save($node);
}

For more details please refer link1, link2

This code is not tested, first just try for a single node by using a for loop instead of foreach.

3
  • 1
    Instead of querying the database for the nids, and loading the nodes. node_load_multiple can be used. Something like $nodes = node_load_multiple(array(), array('type' => 'your_content_type', 'status' => 1)); which would reduce the first for loop and improve the performance though it's a one time process.
    – Suresh R
    Sep 1 '16 at 7:14
  • you are right @SureshR. I missed that.
    – Abin
    Sep 1 '16 at 7:51
  • Thanks a lot @Abin and Suresh R for saving my time... it worked.. Sep 15 '16 at 18:58

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .