Answers.Body: <p>This should do it:</p>
  <pre><code>\Craft::$app-&gt;getElements()-&gt;deleteElement($product);
  </code></pre>
  <p>Then run garbage collection.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-09T16:03:11.183Z
Answers.Text: Ran the command `php craft gc --delete-all-trashed=1` afterwards for the garbage collection. Works great, thanks!
Answers.UserDisplayName: cherrykoda
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-09T15:37:11.963Z
Answers.LastActivityDate: 2023-03-09T15:37:11.963Z
Answers.UserDisplayName: Luke Holder
Body: <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>
  
Tags: <plugin-craftcommerce><products><delete>
Title: Delete All Products (and their variants)
ThreadText: # 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!  
  
  ---
  
  