Reset Magento orders to start at #100000001

16.Jun.2015

During our development today, we offer our clients a server on which they can try and test their future store so they can make a lot of changes, and even prepare for the launch. We call it a staging server.

Once we’re ready for the launch and store is fully developed, it is quite possible that our client has already entered all the products information and uploaded pictures of products. It would be a shame to make them do all this over again, and it’s way to complicated for us to move just bits and pieces. Instead, wouldn’t it be amazing if we could transfer complete database from staging server to live server and just delete all the test orders, test invoices, credit memos, shipments and quotes, as well as test and demo customers?

Now we can with the following code. Enjoy using it on your own project, but be careful, and don’t use it on live server more than once (just before the launch).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
 * Magento store reset script
 *
 * Removes all Orders, Invoices, Credit memos, Shipments, Quotes and Customers
 * Also resets Increment id of sales models back to 100000000
 *
 */
 
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
 
require_once 'lib/Zend/Debug.php';
require_once 'app/Mage.php';
 
Mage::setIsDeveloperMode(true);
Mage::app();
Mage::register('isSecureArea', 1);
 
$config = array(
    'delete_order'      => true,
    'delete_invoice'    => true,
    'delete_creditmemo' => true,
    'delete_shippment'  => true,
    'delete_customers'  => true,
    'delete_quotes'     => true,
    'reset_ids'         => true
);
 
$orderCollection = Mage::getModel('sales/order')->getCollection();
 
foreach ($orderCollection as $order) {
    $order = Mage::getModel('sales/order')->load($order->getId());
 
    if (true === $config['delete_invoice']) {
        foreach ($order->getInvoiceCollection() as $invoice) {
            Mage::getModel('sales/order_invoice')->setId($invoice->getId())->delete();
            echo "Deleted invoice ID:{$invoice->getId()}<br/>";
        }
    }
 
    if (true === $config['delete_creditmemo']) {
        foreach ($order->getCreditmemosCollection() as $creditmemo) {
            Mage::getModel('sales/order_creditmemo')->setId($creditmemo->getId())->delete();
            echo "Deleted creditmemo ID:{$creditmemo->getId()}<br/>";
        }
    }
 
    if (true === $config['delete_shippment']) {
        foreach ($order->getShipmentsCollection() as $shippment) {
            Mage::getModel('sales/order_shipment')->setId($shippment->getId())->delete();
            echo "Deleted shippment ID:{$shippment->getId()}<br/>";
        }
    }
 
    if (true === $config['delete_order']) {
        $order->delete();
        echo "Deleted order ID:{$order->getId()}<br/>";
    }
}
 
if (true === $config['delete_customers']) {
    $customerCollection = Mage::getModel("customer/customer")->getCollection();
    foreach ($customerCollection as $customer) {
            Mage::getModel('customer/customer')->setId($customer->getId())->delete();
            echo "Deleted customer ID:{$customer->getId()}<br/>";
    }
}
 
if (true === $config['delete_quotes']) {
    $quoteCollection = Mage::getModel("sales/quote")->getCollection();
    foreach ($quoteCollection as $quote) {
            Mage::getModel('sales/quote')->setId($quote->getId())->delete();
            echo "Deleted quote ID:{$quote->getId()}<br/>";
    }
}
 
if (true === $config['reset_ids']) {
    $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    $write->query("UPDATE eav_entity_store SET increment_last_id = '100000000';");
}