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

CC BY-SA 4.0
2023-04-19T08:18:33.993Z
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 ) }}`
Mark
CC BY-SA 4.0
2023-04-19T08:32:33.890Z
@Mark Ah right, something like that would be required to remove the last matrix block, good catch!
MoritzLost
CC BY-SA 4.0
2023-04-19T08:36:49.337Z
I've added the `else` statement to my example code, in case someone is looking for a full example.
MoritzLost
CC BY-SA 4.0
2023-04-18T09:48:47.027Z
2023-04-19T08:36:13.200Z
2023-04-19T08:36:13.200Z
MoritzLost
<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>

<craft3><matrix>
Deleting matrix block on front end form
# 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 %}

{{ 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') }}

                          

{% 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:
{% set orders = user.trophyOrders.all() %}
{% set ids = orders|column('id') %}

{% for order in orders %}
    
        {{ 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 %}

        Delete order {{ order.id }}
    
{% 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.  

---

