Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-05T14:25:32.213Z
Answers.Text: 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.
Answers.UserDisplayName: Robin Gauthier
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-05T14:34:18.350Z
Answers.Text: The purgeUnsavedDrafts doesn't seem to work, I need to test furthermore if I can manage something while changing the purgeUnsavedDraftsDuration config.
Answers.UserDisplayName: Robin Gauthier
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-04T21:38:43.187Z
Answers.LastActivityDate: 2023-01-05T21:55:13.313Z
Answers.LastEditDate: 2023-01-05T21:55:13.313Z
Answers.UserDisplayName: RomanAvr
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-05T21:45:05.707Z
Answers.Text: Can you try my updated answer?
Answers.UserDisplayName: RomanAvr
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-06T13:38:26.377Z
Answers.Text: The updated answer works after the entry save.
Answers.UserDisplayName: Robin Gauthier
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-05T16:51:10.047Z
Answers.LastActivityDate: 2023-01-05T16:51:10.047Z
Answers.UserDisplayName: Robin Gauthier
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-01-06T07:05:59.523Z
Comments.Text: 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());
Comments.UserDisplayName: user9975
Tags: <tasks><content><draft>
Title: Auto draft with background changes
ThreadText: # 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.
  
  1. I click on the button in the sidebar
  2. Before the page is redirect to the link, a draft is created for my entry.
  
  [![screenshot1](https://i.sstatic.net/A6CbL.png)](https://i.sstatic.net/A6CbL.png)
  
  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)
  
  [![screenshot2](https://i.sstatic.net/q2B4a.png)](https://i.sstatic.net/q2B4a.png)
  
  **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.  
  
  ---
  
  