Answers.Body: <p>Ok I'm a dummy. A matrix block is an element like an <code>Entry</code>, <code>Asset</code>, etc. So I need to delete it like an element instead of treat it like an array:</p>
  
  <pre><code>$blocks = $entry-&gt;myMatrixField-&gt;all();
  
  if (! empty($blocks)) {
  
      $elementsService = Craft::$app-&gt;getElements();
  
      foreach ($blocks as $block) {
          /* @var \craft\elements\MatrixBlock $block */
  
          $elementsService-&gt;deleteElement($block);
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-12T22:52:15.563Z
Answers.LastActivityDate: 2020-06-12T22:52:15.563Z
Answers.UserDisplayName: Ryan
Body: <p>I want to <strong>reset</strong> a matrix field to have no blocks in it. In a plugin, I would normally <strong>set</strong> a matrix field by doing something like this:</p>
  
  <pre><code>$blocks = [];
  $i = 1;
  foreach ($itemsArray as $item) {
      $blocks['new' . $i] = [
          'type' =&gt; 'myBlockTypeHandle',
          'fields' =&gt; [
              'someBlockFieldHandle1' =&gt; $item['value1'],
              'someBlockFieldHandle2' =&gt; $item['value2'],
              'someBlockFieldHandle3' =&gt; $item['value3'],
          ],
      ];
      ++$i;
  }
  
  $entry-&gt;setFieldValues([
      'myMatrixField' =&gt; $blocks,
  ]);
  Craft::$app-&gt;elements-&gt;saveElement($entry);
  </code></pre>
  
  <p>But I want to do the opposite and set the matrix field to have <strong>no</strong> blocks. I have tried setting the field to an empty array but it doesn't work:</p>
  
  <pre><code>$entry-&gt;setFieldValues([
      'myMatrixField' =&gt; [],
  ]);
  Craft::$app-&gt;elements-&gt;saveElement($entry);
  </code></pre>
  
  <p>There are no errors on save but the empty array gets ignored and my matrix field still has its data in it. I've tried setting it to empty nested arrays and to <code>null</code> but they all get ignored.</p>
  
  <p>How do I reset a matrix field?</p>
  
Tags: <plugin-development><matrix>
Title: How to reset matrix field to have NO blocks in plugin?
ThreadText: # How to reset matrix field to have NO blocks in plugin?
  
  (Asked by: username_0 on 2020-06-12)
  
  I want to **reset** a matrix field to have no blocks in it. In a plugin, I would normally **set** a matrix field by doing something like this:
  
  ```
  $blocks = [];
  $i = 1;
  foreach ($itemsArray as $item) {
      $blocks['new' . $i] = [
          'type' => 'myBlockTypeHandle',
          'fields' => [
              'someBlockFieldHandle1' => $item['value1'],
              'someBlockFieldHandle2' => $item['value2'],
              'someBlockFieldHandle3' => $item['value3'],
          ],
      ];
      ++$i;
  }
  
  $entry->setFieldValues([
      'myMatrixField' => $blocks,
  ]);
  Craft::$app->elements->saveElement($entry);
  
  ```
  
  But I want to do the opposite and set the matrix field to have **no** blocks. I have tried setting the field to an empty array but it doesn't work:
  
  ```
  $entry->setFieldValues([
      'myMatrixField' => [],
  ]);
  Craft::$app->elements->saveElement($entry);
  
  ```
  
  There are no errors on save but the empty array gets ignored and my matrix field still has its data in it. I've tried setting it to empty nested arrays and to `null` but they all get ignored.
  
  How do I reset a matrix field?
  
  **Tags:** plugin-development, matrix
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-06-12. Score: 1
  
  Ok I'm a dummy. A matrix block is an element like an `Entry`, `Asset`, etc. So I need to delete it like an element instead of treat it like an array:
  
  ```
  $blocks = $entry->myMatrixField->all();
  
  if (! empty($blocks)) {
  
      $elementsService = Craft::$app->getElements();
  
      foreach ($blocks as $block) {
          /* @var \craft\elements\MatrixBlock $block */
  
          $elementsService->deleteElement($block);
      }
  }
  
  ```
  
  
  ---
  
  