Magento – Delete all cancelled magento orders

There are many plug-ins are available that would add option to delete order in grid actions drop-down. But I had some requirement toset a cron job that would delete all canceled orders. We know well how to create cron job, if not this post will help you to create cron job in magento.

You can add bellow code to your model funtion that would be run by the cron job,

$collection = Mage::getResourceModel('sales/order_collection')
            ->addFieldToFilter('status', 'canceled')

foreach ($collection as $col) {
  Mage::log($col->getIncrementId() . ' order deleted ');
    try {
    } catch (Exception $e) {
        throw $e;

Create cron job in Magento

Create cronjob script in magento is very simple task, Which I have explain bellow with sample module called “CRM”.

First of all create a module. Now in module’s config.xml we will setup cron tab as bellow.

                <schedule><cron_expr>*/45 * * * *</cron_expr></schedule>

In above code,

<crm_customer_update> – is unique identifier, will be used as job_code in cron_schedule DB table

<schedule><cron_expr>* * * * *</cron_expr></schedule> – each asterisk stands for time period correspondingly: minutes, hours, days, months, years. If you leave it this way (* * * * *), cron job will be executed every minute. In our example, cron job will be run every 45 minutes <schedule><cron_expr>*/45 * * * *</cron_expr></schedule> or we can use alternative syntax to do that <schedule><cron_expr>0,15,30,45 * * * *</cron_expr></schedule>. If you want, for example, to run your job at 5 a.m., you need to do your cron expresion this way: <schedule><cron_expr>0 5 * * *</cron_expr></schedule>

Now the actual code for cron job will be unde,


class Mycode_Crm_Model_Customer extends Mage_Core_Model_Abstract
   public function sendUpdates() {
     //code here to be cron