Skip to main content
Delete an order

How to delete an order if the order is in cart for N days

In drupal commerce, the order will be created when a product is added to cart and the order will exist until the order is deleted or order status is changed to completed.

We have an e-commerce site in which most of the orders are idle in the shopping cart. So we decided to delete the order if the order is in the shopping cart for more than 'N' days or weeks. I have written a custom drush command for deleting the order.

The following code is used to delete an order if it is in the cart for more than 25 days.


* Implements hook_drush_command().


function dt_admin_drush_command() {

 $items = array();

 // Name of the drush command.

 $items['order-delete'] = array(

   'description' => 'Delete the order if order is in cart for 3 months',

   'callback' => 'drush_order_delete',


 return $items;


function drush_order_delete() {

//  Load all order which is in the cart.

 $orders = commerce_order_load_multiple(array(), array('status'=>'cart'), TRUE);

 foreach ($orders as $order) {

    // check order time.

   if ((REQUEST_TIME - $order->changed) >= (25 * 60 * 60 * 24)) {

     $order_ids[] = $order->order_id;




To delete the order which is the cart for more than 25 days, use the following drush command:

drush order-delete

For more details about creating custom drush command, visit this blog.