0

There is a content type called library which has a field collection type as a field and unlimited number of this field is allowed. I used Devel module to generate 200 nodes of library, so more than 1000 field collection items were generated. after deleting the nodes from /admin/content the field collection items were not deleted. How do I can delete these field collection items?

After a few search I found this code

for($i = 1 ; $i <= 700 ; $i++)
    entity_delete_multiple('field_collection_item', $i);

but it deleted nothing.I also found a few other codes on Drupal Stackexchange but none of them worked.

Thanks

  • is the field collection field shared across multiple content types? – Geoff Jun 6 '15 at 17:37
  • No, just library – M a m a D Jun 7 '15 at 3:21
0

You could delete the field collection records from there – either through custom Drupal code, or through direct database access if you have that.

Since you have devel enabled and you are generating content, I'm assuming you're not working on a production site, but it's worth saying that deleting records from the database is a risky proposition.

I'm also assuming that you want to delete all of the field collection data for this field collection and not cherry pick just the content that was generated by devel.

You didn't mention what your field collection or its fields were named, but let's say that you have a field collection with the machine-name testcollection and in that collection you have two fields with machine-names collectionfield and collectionfield2.

The data for these will be stored in several tables. In my example, collectionfield1 and collectionfield2 are text fields. Therefore the data will be stored like so:

{field_data_field_testcollection}        // the field collection
{field_revision_field_testcollection}    // the field collection (revisions)
{field_data_field_collectionfield}       // field attached to "testcollection"
{field_data_field_collectionfield2}      // field2 attached to "testcollection"
{field_revision_field_collectionfield}   // revisions
{field_revision_field_collectionfield2}  // revisions

You could remove the fieldcollection content by running the following queries:

DELETE FROM field_data_field_collectionfield WHERE bundle = 'field_testcollection';

DELETE FROM field_data_field_collectionfield2 WHERE bundle = 'field_testcollection';

DELETE FROM field_revision_field_collectionfield WHERE bundle = 'field_testcollection';

DELETE FROM field_revision_field_collectionfield2 WHERE bundle = 'field_testcollection';

If you have data in the testcollection field even after deleting the generated nodes, you should delete that too:

DELETE FROM field_data_field_testcollection;

DELETE FROM field_revision_field_testcollection;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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