1

I'm looking for the correct way to delete all of the products and related variants from my Commerce site. This is just for my local development environment as I'm debugging an import job. The delete() function is the wrong code in the following console controller:

public function actionNuke()
{
    $products = Product::findAll();
    foreach($products as $product){
        $product->delete();
    }
    $this->stdout("Deleted All Products", Console::FG_GREEN);
    return ExitCode::OK;
}

1 Answer 1

3

This should do it:

\Craft::$app->getElements()->deleteElement($product);

Then run garbage collection.

1
  • 1
    Ran the command php craft gc --delete-all-trashed=1 afterwards for the garbage collection. Works great, thanks!
    – cherrykoda
    Mar 9 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.