How to Delete Test Data Programmatically in Magento 2

As I mentioned in my previous blog (Magento : Clean All the Test Data After Development), While developing a Magento 2 website, the developer and tester may add lots of data (i.e, products, customers, orders etc) for unit testing and for system testing. After finishing the development, when we move the website to live, we might need to clear all those test data. But we should delete the test data in the proper way. The codes below will help you to clear the data and providing a clean database for production.

Note: Just in case, please take a backup of your test DB before applying this code.

Delete Products:

Here the code to delete the product by id programmatically in Magento 2,

$productID = 3;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productID);
$product->delete();

Delete Categories:

Here the code to delete the category by id programmatically in Magento 2,

$categoryID = 5;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryID);
$category->delete();

Delete Customers:

Here the code to delete the customer by id programmatically in Magento 2,

$customerID = 5;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerID);
$customer->delete();

Delete Orders:

Here the code to delete the Orders, Invoice, Credit Memo and Shipment programmatically in Magento 2,

$orderIds = [1, 2, 3];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orders = $objectManager->create('Magento\Sales\Model\Order')->getCollection()->addFieldToFilter('entity_id', $orderIds);

foreach ($orders as $o) {

    $order = $objectManager->create('Magento\Sales\Model\Order')->load($o->getId());

    //delete all order items
    $items = $order->getAllItems();
    foreach ($items as $item) {
        $item->delete();
    }

    $invoices = $order->getInvoiceCollection();
    foreach ($invoices as $invoice){

        //delete all invoice items
        $items = $invoice->getAllItems();
        foreach ($items as $item) {
            $item->delete();
        }

        //delete invoice
        $invoice->delete();
    }

    $creditnotes = $order->getCreditmemosCollection();
    foreach ($creditnotes as $creditnote){

        //delete all creditnote items
        $items = $creditnote->getAllItems();
        foreach ($items as $item) {
            $item->delete();
        }

        //delete credit note
        $creditnote->delete();
    }

    $shipments = $order->getShipmentsCollection();
    foreach ($shipments as $shipment){

        //delete all shipment items
        $items = $shipment->getAllItems();
        foreach ($items as $item) {
            $item->delete();
        }

        //delete shipment
        $shipment->delete();
    }

    $order->delete();
}

Delete Cart Items:

Here the code to delete the cart items programmatically in Magento 2,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$checkoutSession = $objectManager->get('Magento\Checkout\Model\Session');
$cart = $objectManager->create('Magento\Checkout\Model\Cart');
$allItems = $checkoutSession->getQuote()->getItemsCollection();

foreach ($allItems as $item) {
    $itemId = $item->getItemId();
    $cart->removeItem($itemId);
    $cart->save();
}

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *