Created
February 9, 2017 21:55
-
-
Save doostinharrell/d2aa015bcdf8e46d987c6cadfb51ab7e to your computer and use it in GitHub Desktop.
Clean out a Drupal Commerce installation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Deletes all user, term, node, and commerce data to clean out a website. | |
$entity_types = array( | |
'user' => array( | |
'entity_type' => 'user', | |
'id_field' => 'uid', | |
), | |
'taxonomy_term' => array( | |
'entity_type' => 'taxonomy_term', | |
'id_field' => 'tid', | |
), | |
'commerce_order' => array( | |
'entity_type' => 'commerce_order', | |
'id_field' => 'order_id', | |
), | |
'commerce_line_item' => array( | |
'entity_type' => 'commerce_line_item', | |
'id_field' => 'line_item_id', | |
), | |
'commerce_customer_profile' => array( | |
'entity_type' => 'commerce_customer_profile', | |
'id_field' => 'profile_id', | |
), | |
'commerce_payment_transaction' => array( | |
'entity_type' => 'commerce_payment_transaction', | |
'id_field' => 'transaction_id', | |
), | |
'commerce_product' => array( | |
'entity_type' => 'commerce_product', | |
'id_field' => 'product_id', | |
), | |
'node' => array( | |
'entity_type' => 'node', | |
'id_field' => 'nid', | |
'bundle' => array( | |
'name' => 'type', | |
'value' => 'product', | |
), | |
), | |
); | |
// UID's to ignore. | |
$ignored_uids = array(0, 1); | |
foreach($entity_types as $key => $entity_type) { | |
$efq = new EntityFieldQuery(); | |
$efq->entityCondition('entity_type', $entity_type['entity_type']); | |
// Append budle information to the EFQ if provided. | |
if (!empty($entity_type['bundle'])) { | |
$efq->propertyCondition($entity_type['bundle']['name'], $entity_type['bundle']['value']); | |
}; | |
$results = $efq->execute(); | |
foreach ($results[$entity_type['entity_type']] as $result) { | |
if ($key == 'user' && in_array($result->uid, $ignored_uids)) { | |
// do nothing | |
} | |
else { | |
entity_delete($entity_type['entity_type'], $result->{$entity_type['id_field']}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment