<p>If I understand you correctly, this can work?:</p>
<pre><code>if ($restraunt-&gt;isDraft) {
  return; // Skip draft
}
</code></pre>
<p>Or if not and you really want to purge ALL drafts, you can do this:</p>
<pre><code>use craft\services\Drafts;
...

((new Drafts())-&gt;purgeUnsavedDrafts());
</code></pre>
<p>Just to clarify, does your button fire custom controller? Or are you saving data from the API in the event hook?</p>
<p><strong>UPD</strong>:</p>
<p>Can you try this after you have saved your entry?</p>
<p>I just stole this piece of code from the entry controller</p>
<pre><code>$provisional = Entry::find()
            -&gt;provisionalDrafts()
            -&gt;draftOf($restraunt-&gt;id)
            -&gt;siteId($restraunt-&gt;siteId)
            -&gt;anyStatus()
            -&gt;one();
if ($provisional) {
            Craft::$app-&gt;getElements()-&gt;deleteElement($provisional, true);
 }
</code></pre>

CC BY-SA 4.0
2023-01-05T14:25:32.213Z
I 've edited my post with more informations. The button fire a custom controller that will create a job in a queue to update the entry.
Robin Gauthier
CC BY-SA 4.0
2023-01-05T14:34:18.350Z
The purgeUnsavedDrafts doesn't seem to work, I need to test furthermore if I can manage something while changing the purgeUnsavedDraftsDuration config.
Robin Gauthier
CC BY-SA 4.0
2023-01-04T21:38:43.187Z
2023-01-05T21:55:13.313Z
2023-01-05T21:55:13.313Z
RomanAvr
<p>I found a temporary workaround by deleting draft from my entry Id with a simple query.</p>
<pre><code>use craft\db\Query;
use craft\db\Table;
    
(new Query)
     -&gt;createCommand()
     -&gt;delete(Table::DRAFTS, ['canonicalId' =&gt; $entry-&gt;id])
     -&gt;execute();
</code></pre>

CC BY-SA 4.0
2023-01-05T21:45:05.707Z
Can you try my updated answer?
RomanAvr
CC BY-SA 4.0
2023-01-06T13:38:26.377Z
The updated answer works after the entry save.
Robin Gauthier
CC BY-SA 4.0
2023-01-05T16:51:10.047Z
2023-01-05T16:51:10.047Z
Robin Gauthier
<p>I have an entry with a readonly json field (Jason plugin). This field is populate with data from an external api. So I have a button in my entry page that call a custom route which call the api and save the data in the entry. This part works fine.</p>
<p>My problem is when I click on my button, a draft is created before action is done. Since I'm not using the save button from craft, the draft is not deleted and when I go back to the entry I have this notice:</p>
<blockquote>
<p>Recent changes to the Current revision have been merged into this draft.</p>
</blockquote>
<p>But the json data is not updated. If I cancel the changes (Showing your unsaved changes), the entry is fine.</p>
<p>Is there a way to avoid the creation of the draft when I click the button, or if I can delete the current in my custom code before saving the data?</p>
<p>Here's a snippet of the save:</p>
<pre><code>$restaurant = Entry::find()
                -&gt;section('restaurants')
                -&gt;slug('restaurant-1')
                -&gt;one();
$restaurant-&gt;setFieldValue('restaurant_schedules', Json::encode($schedules));
Craft::$app-&gt;elements-&gt;saveElement($restaurant);
</code></pre>
<p>Edit 1 :</p>
<p>The button is a simple route with the entry slug in the url that called this controller. (/sync/schedule/my-entry-slug) This controller create a new job in the queue that will update the entry.</p>
<pre><code>public function actionSync($restaurantSlug)
{
    $restaurantService = new RestaurantsService();
    $restaurant = $restaurantService-&gt;getRestaurantBySlug($restaurantSlug);

    Queue::push(new ScheduleSingleJob([
                    'restaurant' =&gt; $restaurant,
                    ]),
                self::JOBS_PRIORITY
    );

    return $this-&gt;redirect('/admin/entries/restaurants');
}

</code></pre>
<p>Here's some screenshots of my steps.</p>
<ol>
<li>I click on the button in the sidebar</li>
<li>Before the page is redirect to the link, a draft is created for my entry.</li>
</ol>
<p><a href="https://i.sstatic.net/A6CbL.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/A6CbL.png" alt="screenshot1" /></a></p>
<p>and when I come back after the queue is done, the draft is shown without the changes applied to the entry. (I need to click cancel draft to see them)</p>
<p><a href="https://i.sstatic.net/q2B4a.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/q2B4a.png" alt="screenshot2" /></a></p>

CC BY-SA 4.0
2023-01-06T07:05:59.523Z
Hi. it seems the job is about one restaurant. so what if update and save the restaurant element in sync action and redirect to edit page like :  return $this->redirect($restaurant->getCpEditUrl());
user9975
<tasks><content><draft>
Auto draft with background changes
# Auto draft with background changes

(Asked by: username_0 on 2023-01-04)

I have an entry with a readonly json field (Jason plugin). This field is populate with data from an external api. So I have a button in my entry page that call a custom route which call the api and save the data in the entry. This part works fine.
My problem is when I click on my button, a draft is created before action is done. Since I'm not using the save button from craft, the draft is not deleted and when I go back to the entry I have this notice:

Recent changes to the Current revision have been merged into this draft.

But the json data is not updated. If I cancel the changes (Showing your unsaved changes), the entry is fine.
Is there a way to avoid the creation of the draft when I click the button, or if I can delete the current in my custom code before saving the data?
Here's a snippet of the save:
$restaurant = Entry::find()
                ->section('restaurants')
                ->slug('restaurant-1')
                ->one();
$restaurant->setFieldValue('restaurant_schedules', Json::encode($schedules));
Craft::$app->elements->saveElement($restaurant);

Edit 1 :
The button is a simple route with the entry slug in the url that called this controller. (/sync/schedule/my-entry-slug) This controller create a new job in the queue that will update the entry.
public function actionSync($restaurantSlug)
{
    $restaurantService = new RestaurantsService();
    $restaurant = $restaurantService->getRestaurantBySlug($restaurantSlug);

    Queue::push(new ScheduleSingleJob([
                    'restaurant' => $restaurant,
                    ]),
                self::JOBS_PRIORITY
    );

    return $this->redirect('/admin/entries/restaurants');
}


Here's some screenshots of my steps.

I click on the button in the sidebar
Before the page is redirect to the link, a draft is created for my entry.


and when I come back after the queue is done, the draft is shown without the changes applied to the entry. (I need to click cancel draft to see them)

**Tags:** tasks, content, draft
> **Comments:**
> **username_1**: Hi. it seems the job is about one restaurant. so what if update and save the restaurant element in sync action and redirect to edit page like :  return $this->redirect($restaurant->getCpEditUrl());  

---

## 2 answers

### Answer by username_2 on 2023-01-04. Score: 2 | Accepted answer

If I understand you correctly, this can work?:
if ($restraunt->isDraft) {
  return; // Skip draft
}

Or if not and you really want to purge ALL drafts, you can do this:
use craft\services\Drafts;
...

((new Drafts())->purgeUnsavedDrafts());

Just to clarify, does your button fire custom controller? Or are you saving data from the API in the event hook?
UPD:
Can you try this after you have saved your entry?
I just stole this piece of code from the entry controller
$provisional = Entry::find()
            ->provisionalDrafts()
            ->draftOf($restraunt->id)
            ->siteId($restraunt->siteId)
            ->anyStatus()
            ->one();
if ($provisional) {
            Craft::$app->getElements()->deleteElement($provisional, true);
 }

> **Comments:**
> **username_0**: I 've edited my post with more informations. The button fire a custom controller that will create a job in a queue to update the entry.  
> **username_0**: The purgeUnsavedDrafts doesn't seem to work, I need to test furthermore if I can manage something while changing the purgeUnsavedDraftsDuration config.  

---

### Answer by username_0 on 2023-01-05. Score: 1

I found a temporary workaround by deleting draft from my entry Id with a simple query.
use craft\db\Query;
use craft\db\Table;
    
(new Query)
     ->createCommand()
     ->delete(Table::DRAFTS, ['canonicalId' => $entry->id])
     ->execute();

> **Comments:**
> **username_2**: Can you try my updated answer?  
> **username_0**: The updated answer works after the entry save.  

---

