<p>This should do it:</p>
<pre><code>\Craft::$app-&gt;getElements()-&gt;deleteElement($product);
</code></pre>
<p>Then run garbage collection.</p>

CC BY-SA 4.0
2023-03-09T16:03:11.183Z
Ran the command `php craft gc --delete-all-trashed=1` afterwards for the garbage collection. Works great, thanks!
cherrykoda
CC BY-SA 4.0
2023-03-09T15:37:11.963Z
2023-03-09T15:37:11.963Z
Luke Holder
<p>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:</p>
<pre><code>public function actionNuke()
{
    $products = Product::findAll();
    foreach($products as $product){
        $product-&gt;delete();
    }
    $this-&gt;stdout(&quot;Deleted All Products&quot;, Console::FG_GREEN);
    return ExitCode::OK;
}
</code></pre>

<plugin-craftcommerce><products><delete>
Delete All Products (and their variants)
# Delete All Products (and their variants)

(Asked by: username_0 on 2023-03-09)

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;
}

**Tags:** plugin-craftcommerce, products, delete

---

## 1 answers

### Answer by username_1 on 2023-03-09. Score: 3 | Accepted answer

This should do it:
\Craft::$app->getElements()->deleteElement($product);

Then run garbage collection.

> **Comments:**
> **username_0**: Ran the command `php craft gc --delete-all-trashed=1` afterwards for the garbage collection. Works great, thanks!  

---

