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

CC BY-SA 4.0
2021-06-18T15:43:56.013Z
Heroic answer, thank you very much!
Jake
CC BY-SA 4.0
2021-06-18T07:28:30.333Z
2021-06-18T07:36:37.760Z
2021-06-18T07:36:37.760Z
MoritzLost
<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>

<matrix>
How do you delete a matrix block in module/plugin assuming you have the matrix block id and entry
# 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 to delete any element, including matrix blocks:
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()
craft\services\Elements::deleteElementById()

> **Comments:**
> **username_0**: Heroic answer, thank you very much!  

---

