Answers.Body: <p>I'm not sure how that code is supposed to work. The <code>sortOrder</code> controls which blocks (both existing and new) get saved in what order. The input in your form includes <em>only</em> the ID of the current block in the loop, so it makes sense that all other blocks will be deleted.</p>
  <p>You want to achieve the opposite – keep all blocks except the current block in the loop. So you need to output a <code>sortOrder</code> that includes all existing IDs <em>except</em> the current one.</p>
  <p>This code should work:</p>
  <pre class="lang-twig prettyprint-override"><code>{% set orders = user.trophyOrders.all() %}
  {% set ids = orders|column('id') %}
  
  {% for order in orders %}
      &lt;form method=&quot;post&quot; accept-charset=&quot;UTF-8&quot;&gt;
          {{ csrfInput() }}
          {{ actionInput('users/save-user') }}
          {{ hiddenInput('userId', currentUser.id) }}
  
          {% for id in ids|filter(id =&gt; id != order.id) %}
              {{ hiddenInput('fields[trophyOrders][sortOrder][]', id) }}
          {% else %}
              {{ hiddenInput('fields[trophyOrders][sortOrder][]', null) }}
          {% endfor %}
  
          &lt;button type=&quot;submit&quot; class=&quot;btn-unstyled&quot;&gt;Delete order {{ order.id }}&lt;/button&gt;
      &lt;/form&gt;
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-19T08:18:33.993Z
Answers.Text: I added an else statement for when it's the last block, which seems to work. Thanks for your help, again.
  
  `{% else %}             {{ hiddenInput('fields[trophyOrders][sortOrder][]', null ) }}`
Answers.UserDisplayName: Mark
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-19T08:32:33.890Z
Answers.Text: @Mark Ah right, something like that would be required to remove the last matrix block, good catch!
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-19T08:36:49.337Z
Answers.Text: I've added the `else` statement to my example code, in case someone is looking for a full example.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-18T09:48:47.027Z
Answers.LastActivityDate: 2023-04-19T08:36:13.200Z
Answers.LastEditDate: 2023-04-19T08:36:13.200Z
Answers.UserDisplayName: MoritzLost
Body: <p>The below code doesn't appear to be working for me, it will delete all other blocks apart from itself. I just need it to delete itself.</p>
  <p><code>trophyOrders</code> is the Matrix field in the user account.</p>
  <pre><code>{% set orders = user.trophyOrders.all() %}
  
  {# Loop through orders #}
  {% for order in orders %}
  &lt;form method=&quot;post&quot; accept-charset=&quot;UTF-8&quot; id=&quot;form-{{ order.id }}&quot; class=&quot;f-left&quot;&gt;
  {{ csrfInput() }}
  {{ actionInput('users/save-user') }}
  {{ redirectInput('account/manage-trophies?msg=entry-deleted') }}
  {{ hiddenInput('userId', currentUser.id) }}
                                                          
  {{ hiddenInput('fields[trophyOrders][sortOrder][]', order.id) }}
  {{ hiddenInput('fields[trophyOrders][blocks][' ~ order.id ~'][type]', '0') }}
  
  &lt;button type=&quot;submit&quot; class=&quot;btn-unstyled&quot;&gt;&lt;i class=&quot;fal fa-trash-alt&quot;&gt;&lt;/i&gt;&lt;/button&gt;                          
  &lt;/form&gt;
  {% endfor %}
  </code></pre>
  <p>Thanks</p>
  
Tags: <craft3><matrix>
Title: Deleting matrix block on front end form
ThreadText: # Deleting matrix block on front end form
  
  (Asked by: username_0 on 2023-04-17)
  
  The below code doesn't appear to be working for me, it will delete all other blocks apart from itself. I just need it to delete itself.
  
  `trophyOrders` is the Matrix field in the user account.
  
  ```
  {% set orders = user.trophyOrders.all() %}
  
  {# Loop through orders #}
  {% for order in orders %}
  <form method="post" accept-charset="UTF-8" id="form-{{ order.id }}" class="f-left">
  {{ csrfInput() }}
  {{ actionInput('users/save-user') }}
  {{ redirectInput('account/manage-trophies?msg=entry-deleted') }}
  {{ hiddenInput('userId', currentUser.id) }}
                                                          
  {{ hiddenInput('fields[trophyOrders][sortOrder][]', order.id) }}
  {{ hiddenInput('fields[trophyOrders][blocks][' ~ order.id ~'][type]', '0') }}
  
  <button type="submit" class="btn-unstyled"><i class="fal fa-trash-alt"></i></button>                          
  </form>
  {% endfor %}
  
  ```
  
  Thanks
  
  **Tags:** craft3, matrix
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-04-18. Score: 1 | Accepted answer
  
  I'm not sure how that code is supposed to work. The `sortOrder` controls which blocks (both existing and new) get saved in what order. The input in your form includes *only* the ID of the current block in the loop, so it makes sense that all other blocks will be deleted.
  
  You want to achieve the opposite – keep all blocks except the current block in the loop. So you need to output a `sortOrder` that includes all existing IDs *except* the current one.
  
  This code should work:
  
  ```twig
  {% set orders = user.trophyOrders.all() %}
  {% set ids = orders|column('id') %}
  
  {% for order in orders %}
      <form method="post" accept-charset="UTF-8">
          {{ csrfInput() }}
          {{ actionInput('users/save-user') }}
          {{ hiddenInput('userId', currentUser.id) }}
  
          {% for id in ids|filter(id => id != order.id) %}
              {{ hiddenInput('fields[trophyOrders][sortOrder][]', id) }}
          {% else %}
              {{ hiddenInput('fields[trophyOrders][sortOrder][]', null) }}
          {% endfor %}
  
          <button type="submit" class="btn-unstyled">Delete order {{ order.id }}</button>
      </form>
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: I added an else statement for when it's the last block, which seems to work. Thanks for your help, again.  `{% else %}             {{ hiddenInput('fields[trophyOrders][sortOrder][]', null ) }}`  
  > **username_1**: @username_0 Ah right, something like that would be required to remove the last matrix block, good catch!  
  > **username_1**: I've added the `else` statement to my example code, in case someone is looking for a full example.  
  
  ---
  
  