Answers.Body: <p>You can use the <a href="https://docs.craftcms.com/api/v3/craft-services-elements.html" rel="noreferrer">Elements service</a> to delete any element, including matrix blocks:</p>
  <pre class="lang-php prettyprint-override"><code>use craft\elements\MatrixBlock;
  
  /** @var craft\services\Elements $elementsService */
  $elementsService = Craft::$app-&gt;getElements();
  
  $myMatrixBlockId = 123;
  
  // delete a matrix block directly
  $matrixBlock = MatrixBlock::find()-&gt;id($myMatrixBlockId)-&gt;one();
  $elementsService-&gt;deleteElement($matrixBlock, false);
  
  // delete a matrix block by ID
  $elementsService-&gt;deleteElementById($myMatrixBlockId, null, null, false);
  </code></pre>
  <p>The second argument to <code>deleteElement</code> (and the fourth argument to <code>deleteElementById</code>) controls if you want a hard delete (<code>true</code>) or a soft-delete (<code>false</code>). A hard delete removes the block from the database completely, while a soft delete keeps it in the database and only marks it as deleted, allowing it to be restored.</p>
  <ul>
  <li><a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#method-deleteelement" rel="noreferrer">craft\services\Elements::deleteElement()</a></li>
  <li><a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#method-deleteelementbyid" rel="noreferrer">craft\services\Elements::deleteElementById()</a></li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-18T15:43:56.013Z
Answers.Text: Heroic answer, thank you very much!
Answers.UserDisplayName: Jake
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-18T07:28:30.333Z
Answers.LastActivityDate: 2021-06-18T07:36:37.760Z
Answers.LastEditDate: 2021-06-18T07:36:37.760Z
Answers.UserDisplayName: MoritzLost
Body: <p>I have the channel entry to which the matrix block belongs and I have the ID of the block itself. Does anybody know the correct approach for deleting the matrix block on the entry?</p>
  <p>Context: module (PHP) not template.</p>
  
Tags: <matrix>
Title: How do you delete a matrix block in module/plugin assuming you have the matrix block id and entry
ThreadText: # How do you delete a matrix block in module/plugin assuming you have the matrix block id and entry
  
  (Asked by: username_0 on 2021-06-17)
  
  I have the channel entry to which the matrix block belongs and I have the ID of the block itself. Does anybody know the correct approach for deleting the matrix block on the entry?
  
  Context: module (PHP) not template.
  
  **Tags:** matrix
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-06-18. Score: 5 | Accepted answer
  
  You can use the [Elements service](https://docs.craftcms.com/api/v3/craft-services-elements.html) to delete any element, including matrix blocks:
  
  ```php
  use craft\elements\MatrixBlock;
  
  /** @var craft\services\Elements $elementsService */
  $elementsService = Craft::$app->getElements();
  
  $myMatrixBlockId = 123;
  
  // delete a matrix block directly
  $matrixBlock = MatrixBlock::find()->id($myMatrixBlockId)->one();
  $elementsService->deleteElement($matrixBlock, false);
  
  // delete a matrix block by ID
  $elementsService->deleteElementById($myMatrixBlockId, null, null, false);
  
  ```
  
  The second argument to `deleteElement` (and the fourth argument to `deleteElementById`) controls if you want a hard delete (`true`) or a soft-delete (`false`). A hard delete removes the block from the database completely, while a soft delete keeps it in the database and only marks it as deleted, allowing it to be restored.
  
  * [craft\services\Elements::deleteElement()](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-deleteelement)
  * [craft\services\Elements::deleteElementById()](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-deleteelementbyid)
  
  > **Comments:**
  > **username_0**: Heroic answer, thank you very much!  
  
  ---
  
  