How to remove old revisions product? I have a lot of old revisions of products. They must be removed. How?

  • Why must they be removed? – nvahalik Feb 1 '16 at 2:44

Drupal Commerce does not provide a UI nor function for this actually. However, we can look at how the Node module does it and run the same function, just adjusted.

From node.pages.inc, there is a function to delete a specific Node revision.

But that is only for a specific revision ID. Here's an adjusted function that loads a products revisions and purges them.

 * Deletes a node revision.
 * @param $revision_id
 *   The revision ID to delete.
 * @return bool
 *   True on success, false if not.
function commerce_product_revision_delete($revision_id) {
  if ($revision = entity_load('commerce_product', NULL, array('revision_id' => $revision_id))) {
    $revision = reset($revision);
    // Prevent deleting the current revision.
    $commerce_product = commerce_product_load($revision->product_id);
    if ($revision_id == $commerce_product->revision_id) {
      return FALSE;

      ->condition('product_id', $revision->product_id)
      ->condition('revision_id', $revision->revision_id)
    field_attach_delete_revision('commerce_product', $revision);
    return TRUE;
  return FALSE;

Basically the trick is ensuring you load the revision and then delete it's record and invoke field_attach_delete_revision so the old field data gets purged.

I created an issue in the queue for this: https://www.drupal.org/node/2660056


If you're wanting to remove any entity revision, the entity module provides a helper function:

entity_revision_delete($entity_type, $revision_id);

Which you can call like this:

entity_revision_delete('commerce_product', 6);

Where 6 is the revision ID of the product you want to delete.

Views Bulk Operations includes a revision delete option, which you could use to create a view that looks at product revisions and allows you to delete them in bulk.

  • 2
    Drupal Commerce does not implement revision deletion callback callback or controller method, so unfortunately we can't piggy back this. – Matt Glaman Feb 1 '16 at 2:58

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.