Magento 2: Remove Test Data Programmatically

In this tutorial, you will go through how to easily remove test data programmatically in magento 2. This trick will be very useful and handy when you want to remove test data programmatically in Magento 2.

During the development phase, testers and developers may add test data for products, customers, orders, items etc for unit testing and system testing. When the website is set to go live, we need to remove the test data.

Delete Cart Items

You can simply use the below code to delete cart itrems:

/* Clear Cart Data */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$checkoutSession = $objectManager->get('Magento\Checkout\Model\Session');          
$cart = $objectManager->create('Magento\Checkout\Model\Cart');
//returns all cart items in session
$allItems = $checkoutSession->getQuote()->getItemsCollection(); 
foreach ($allItems as $item) {
$itemId = $item->getItemId(); //item id of particular item
$cart->removeItem($itemId); //deletes the item
$cart→save();    
}

Delete Products

You can simply use the below code to delete products by id:

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

Delete Orders

You can simply use the below code to delete orders:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->getCollection()
->addFieldToFilter('entity_id', (array) $orderIds);
foreach ($orders as $o) {
//load order object - I know it's not ok to use load in a loop but it should be ok since it's a one time script
$order = Mage::getModel('sales/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();
}

Delete Customers

You can simply use the below code to delete customers by id:

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

I hope you like this post, Please feel free to comment below, your suggestion and problems!

(Visited 25 times, 1 visits today)
Chandra Kumar

Hello World, I am Chandra Kumar from New Delhi, India. I am professionally developer, programmer and blogger. I am the founder of WebShouters.com.

0 Comment

Leave a Comment

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