<p>That looks perfect!</p>

<p>Obviously this code doesn't prompt the user to confirm deletion, but that can easily be added. (Or perhaps you stripped that out of your example.)</p>

<p>You are specifically doing two things right here:</p>

<ul>
<li><strong>Hitting <code>deleteEntry</code> directly.</strong> This ensures that Craft follows all of its necessary internal procedures to properly delete the entry (and kill any relationships it may have).</li>
<li><strong>Including the CSRF token information.</strong> Not sure if it's currently required for hitting a native Craft method, but it certainly will be when Craft 3 rolls out. Good future-proofing!</li>
</ul>

<p>Looks great Johannes, go for it!</p>

CC BY-SA 3.0
2015-03-20T17:30:00.933Z
It's just a prototype. Don't want an annoying confirm prompt.
Johannes Lamers
CC BY-SA 3.0
2015-03-20T17:27:24.010Z
2015-03-20T17:27:24.010Z
Lindsey D
<p>I'm trying to delete items through the frontend with ajax. It's working now but want to be sure if this is a safe method:</p>

<pre><code>$(".delete-file").click(function(){
    var data = {
        entryId: $(this).attr("data-id"), 
    };

    data[window.csrfTokenName] = window.csrfTokenValue; // Append CSRF Token

    $.ajax({
        context: this,
        type: "post",
        dataType: "json",
        data: data,
        url: "/actions/entries/deleteEntry",
        success: function(data, response){
            $(this).parents('.account-item').remove();
            console.log("success", response, data);
        }
    });
});
</code></pre>

CC BY-SA 3.0
2017-08-24T19:57:33.460Z
This works brilliantly. Now to add the confirmation pop-up.
CreateSean
<entries><form><ajax>
Delete items from frontend with ajax
# Delete items from frontend with ajax

(Asked by: username_0 on 2015-03-20)

I'm trying to delete items through the frontend with ajax. It's working now but want to be sure if this is a safe method:
$(".delete-file").click(function(){
    var data = {
        entryId: $(this).attr("data-id"), 
    };

    data[window.csrfTokenName] = window.csrfTokenValue; // Append CSRF Token

    $.ajax({
        context: this,
        type: "post",
        dataType: "json",
        data: data,
        url: "/actions/entries/deleteEntry",
        success: function(data, response){
            $(this).parents('.account-item').remove();
            console.log("success", response, data);
        }
    });
});

**Tags:** entries, form, ajax
> **Comments:**
> **username_1**: This works brilliantly. Now to add the confirmation pop-up.  

---

## 1 answers

### Answer by username_2 on 2015-03-20. Score: 5 | Accepted answer

That looks perfect!

Obviously this code doesn't prompt the user to confirm deletion, but that can easily be added. (Or perhaps you stripped that out of your example.)

You are specifically doing two things right here:

* **Hitting `deleteEntry` directly.** This ensures that Craft follows all of its necessary internal procedures to properly delete the entry (and kill any relationships it may have).
* **Including the CSRF token information.** Not sure if it's currently required for hitting a native Craft method, but it certainly will be when Craft 3 rolls out. Good future-proofing!

Looks great Johannes, go for it!

> **Comments:**
> **username_0**: It's just a prototype. Don't want an annoying confirm prompt.  

---

