Skip to main content
Blog General

How to delete test data programmatically in Magento 2 ?

Sathishraj Raju
April 11, 2017 |

Providing a clean database is critical when we move to production. 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.

Clearing the data manually is a tedious and a ‘not-a-proper-way’ of cleaning the database.

The codes below clear the data automatically reducing the effort of the developers and also providing a clean database for production.

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

Delete Cart Items:

Delete Products:

Delete Orders:

Code to delete the Orders, Invoice, Credit Memo and Shipment programmatically in Magento 2:

Delete Customers:

Hope the code was useful. Please share us your feedback and comments.

Is your Magento store healthy? Our experts are here to help you with our Magento services for your store. Get in Touch

Sathishraj Raju

Sathish has 6 years of experience in Magento and is a Magento Certified Developer. Having worked across various domains, Sathish is passionate about coding. He loves to play cricket during the weekends and is also an avid body builder.

More posts by Sathishraj Raju