Answers.Body: <p>As mentioned by Oli, I was able to use <code>deleteElementById($entry-&gt;id)</code>. Note $id now refers to $elementId</p>
  <pre><code>/*
   * Find the matching entries to delete
   */
  
  $entry = (new \craft\db\Query())
  -&gt;select(&quot;elementId&quot;)
  -&gt;from(&quot;content&quot;)
  -&gt;where( &quot;title = &quot; . $title )
  -&gt;all();
  
  /*
   * Delete the entry
   */
  foreach( $entry as $key =&gt; $value ) {
  
    // Hard delete.
    $success = Craft::$app-&gt;elements-&gt;deleteElementById( $value['elementId'], null, null , true );
  
  }
  </code></pre>
  <p>Now entries deleted also deleted slugs that were created. But there was one problem: entries deleted previously before this migration code upgrade were still there. I found that these slugs were kept in the table <code>element_sites</code> and I manually deleted all the old slugs no longer being used. So far there has been no issue with this and the rest of the site functions apparently normally.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-31T09:54:04.647Z
Answers.LastActivityDate: 2021-03-31T09:54:04.647Z
Answers.UserDisplayName: Robert
Body: <p>I am migrating some entries into Craft for the first time and my saveUp is quite simple where I use the saveElement function. The problem is after doing a few safeUp's and safeDown's I'm getting some appended numbers on my slugs e.g. 'title-07' when ideally it would just stay as 'title'. The slug is based on the title and it appears my safeDown method doesn't hunt down the old slugs wherever they are.</p>
  <p>My safeUp() looks something like this:</p>
  <pre><code>/*
   * Set the entry and save it
   */
  
  $entry = new Entry();
  
  // Set the entry data
  $entry-&gt;sectionId   = '4';
  $entry-&gt;typeId      = '4';
  $entry-&gt;authorId    = '1';
         
  // Title etc..
  $entry-&gt;title = $title;
  
  // Run saveElement with data
  $success = Craft::$app-&gt;elements-&gt;saveElement($entry);
  </code></pre>
  <p>My safeDown is a bit less elegant:</p>
  <pre><code>/*
   * Find the matching entries to delete
   */
  
  $entry = (new \craft\db\Query())
  -&gt;select(&quot;id&quot;)
  -&gt;from(&quot;content&quot;)
  -&gt;where( &quot;title = &quot; . $title )
  -&gt;all();
  
  /*
   * Loop through matches and delete
   */
  
  foreach( $entry as $key =&gt; $value ) {
  
      $rowsAffected = (new \craft\helpers\Db())
      -&gt;delete('content', 'id=' . $value['id'] );
                 
  }
  </code></pre>
  <p>My question is a couple things.</p>
  <p>Is there something as nice as saveElement where I can just pass a typeId and id for the content and everything related gets removed?</p>
  <p>My current method where I use craft\helpers\Db-&gt;delete leaves behind the original slug. If there's not a cleaner method like saveElement, where can I find the slugs to get rid of them as well?</p>
  <p>Any pointers greatly appreciated.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-03-30T13:39:08.787Z
Comments.Text: Did you try `Craft::$app->elements->deleteElement($entry);` ([docs](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-deleteelement)) or`Craft::$app->elements->deleteElementById($entry->id);` ([docs](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-deleteelementbyid))? As far as I know, this should cascade delete everything for you.
Comments.UserDisplayName: Oli
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-03-30T18:22:58.487Z
Comments.Text: I most certainly did not! Thank you greatly
Comments.UserDisplayName: Robert
Tags: <craft3><delete>
Title: How to remove entries with craft migration safeDown elegantly
ThreadText: # How to remove entries with craft migration safeDown elegantly
  
  (Asked by: username_0 on 2021-03-30)
  
  I am migrating some entries into Craft for the first time and my saveUp is quite simple where I use the saveElement function. The problem is after doing a few safeUp's and safeDown's I'm getting some appended numbers on my slugs e.g. 'title-07' when ideally it would just stay as 'title'. The slug is based on the title and it appears my safeDown method doesn't hunt down the old slugs wherever they are.
  
  My safeUp() looks something like this:
  
  ```
  /*
   * Set the entry and save it
   */
  
  $entry = new Entry();
  
  // Set the entry data
  $entry->sectionId   = '4';
  $entry->typeId      = '4';
  $entry->authorId    = '1';
         
  // Title etc..
  $entry->title = $title;
  
  // Run saveElement with data
  $success = Craft::$app->elements->saveElement($entry);
  
  ```
  
  My safeDown is a bit less elegant:
  
  ```
  /*
   * Find the matching entries to delete
   */
  
  $entry = (new \craft\db\Query())
  ->select("id")
  ->from("content")
  ->where( "title = " . $title )
  ->all();
  
  /*
   * Loop through matches and delete
   */
  
  foreach( $entry as $key => $value ) {
  
      $rowsAffected = (new \craft\helpers\Db())
      ->delete('content', 'id=' . $value['id'] );
                 
  }
  
  ```
  
  My question is a couple things.
  
  Is there something as nice as saveElement where I can just pass a typeId and id for the content and everything related gets removed?
  
  My current method where I use craft\helpers\Db->delete leaves behind the original slug. If there's not a cleaner method like saveElement, where can I find the slugs to get rid of them as well?
  
  Any pointers greatly appreciated.
  
  **Tags:** craft3, delete
  
  > **Comments:**
  > **username_1**: Did you try `Craft::$app->elements->deleteElement($entry);` ([docs](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-deleteelement)) or`Craft::$app->elements->deleteElementById($entry->id);` ([docs](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-deleteelementbyid))? As far as I know, this should cascade delete everything for you.  
  > **username_0**: I most certainly did not! Thank you greatly  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2021-03-31. Score: 0 | Accepted answer
  
  As mentioned by Oli, I was able to use `deleteElementById($entry->id)`. Note $id now refers to $elementId
  
  ```
  /*
   * Find the matching entries to delete
   */
  
  $entry = (new \craft\db\Query())
  ->select("elementId")
  ->from("content")
  ->where( "title = " . $title )
  ->all();
  
  /*
   * Delete the entry
   */
  foreach( $entry as $key => $value ) {
  
    // Hard delete.
    $success = Craft::$app->elements->deleteElementById( $value['elementId'], null, null , true );
  
  }
  
  ```
  
  Now entries deleted also deleted slugs that were created. But there was one problem: entries deleted previously before this migration code upgrade were still there. I found that these slugs were kept in the table `element_sites` and I manually deleted all the old slugs no longer being used. So far there has been no issue with this and the rest of the site functions apparently normally.
  
  
  ---
  
  