Answers.Body: <p>I updated the queries @ColeHenley posted. This should work with Craft 3.x:</p>
  
  <pre><code>DELETE elements FROM ut_entries AS entries, ut_elements_sites AS elements WHERE entries.id = elements.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE relations FROM ut_entries AS entries, ut_relations AS relations WHERE entries.id = relations.sourceId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE revisions FROM ut_entries AS entries, ut_revisions AS revisions WHERE entries.id = revisions.sourceId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE content FROM ut_entries AS entries, ut_content AS content WHERE entries.id = content.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE drafts FROM ut_entries AS entries, ut_drafts AS drafts WHERE entries.id = drafts.sourceId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE search FROM ut_entries AS entries, ut_searchindex AS search WHERE entries.id = search.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE elements FROM ut_entries AS entries, ut_structureelements AS elements WHERE entries.id = elements.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE attributes FROM ut_entries AS entries, ut_changedattributes AS attributes WHERE entries.id = attributes.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE fields FROM ut_entries AS entries, ut_changedfields AS fields WHERE entries.id = fields.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE relations FROM ut_entries AS entries, ut_relations AS relations WHERE (entries.id = relations.sourceId or entries.id = relations.targetId) AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE elements FROM ut_entries AS entries, ut_elements AS elements WHERE entries.id = elements.id AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-29T15:23:19.843Z
Answers.LastActivityDate: 2020-01-29T15:23:19.843Z
Answers.UserDisplayName: Harald Atteneder
Answers.Body: <p>Records are definitely not the way to go with dealing with large amounts of data sets in the database. There is just too much overhead associated with them.</p>
  <p>Manually running SQL (as you've discovered) is an option, but if you wanted to do this from PHP, you could still use <code>QueryBuilder</code>.</p>
  <pre class="lang-php prettyprint-override"><code>$elementIds = craft()-&gt;db-&gt;createCommand()
              -&gt;select('id')
              -&gt;from('entries')
              -&gt;where('sectionId = :sectionId', array(':sectionId' =&gt; 18))
              -&gt;queryColumn();
  
  $rowsAffected = craft()-&gt;db-&gt;createCommand()
              -&gt;delete(
                  'elements',
                  array('in', 'id', $elementIds)
               );
  </code></pre>
  <p>The MySQL cascading deletes setup on the foreign keys should take care of cleaning up the necessary rows in the remaining tables automatically.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-03T10:33:52.293Z
Answers.Text: Hi Brad - sorry for delay in following this up. Is there a typo in your code sample on the `delete()` method?  Seems to be throwing errors here.
Answers.UserDisplayName: Cole Henley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-03T11:10:47.743Z
Answers.Text: @ColeHenley it should have a comma after 'elements' and before the `array()` part. If that works edit the answer as such.
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-03T11:28:23.153Z
Answers.Text: Thanks @JoshAngell - caught that one but couldn't for the life of me see why it wasn't working. Turned out to be a typo - was using createComment rather than createCommand :D
Answers.UserDisplayName: Cole Henley
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2016-01-14T18:33:40.827Z
Answers.LastActivityDate: 2023-03-06T21:11:56.757Z
Answers.LastEditDate: 2023-03-06T21:11:56.757Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm using Feedme to add entries via an external JSON feed.
  There was an issue with matching duplicates and now need to remove &gt;4000 entries from the site.</p>
  <p>To avoid further issues would ideally like to build this into the feedme script we are running but the built-in delete method times out due to memory issues. I've looked at other ways we can achieve this, eg by deleting via the entries model:</p>
  <pre><code>$entriesRecord = EntryRecord::model()-&gt;findAllByAttributes(array('sectionId' =&gt; $sectionId));
  $entriesRecord-&gt;delete();
  </code></pre>
  <p>But this is also timing out. Mike Pepper pointed me in the direction of the <code>a&amp;m command</code> add-on at <a href="https://github.com/am-impact/amcommand" rel="nofollow noreferrer">https://github.com/am-impact/amcommand</a> but this is also struggling. Is the only way to deal with such large quantities of entries to do this directly via SQL?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-01-14T13:13:50.513Z
Comments.Text: Okay - been round circles and several different methods to achieve this.
  Unfortunately with this amount of entries, working directly with the craft model or API just timed out. To clean out the database I had to manually clear out entries from the DB via the following code: https://gist.github.com/cole007/c1c20774510d6a6b5ae5
  
  Note that you will need to execute the `DELETE FROM craft_entries` query last
Comments.UserDisplayName: Cole Henley
Tags: <plugin-development><entrymodel>
Title: Deleting all entries from a Section
ThreadText: # Deleting all entries from a Section
  
  (Asked by: username_0 on 2016-01-14)
  
  I'm using Feedme to add entries via an external JSON feed.
  There was an issue with matching duplicates and now need to remove >4000 entries from the site.
  
  To avoid further issues would ideally like to build this into the feedme script we are running but the built-in delete method times out due to memory issues. I've looked at other ways we can achieve this, eg by deleting via the entries model:
  
  ```
  $entriesRecord = EntryRecord::model()->findAllByAttributes(array('sectionId' => $sectionId));
  $entriesRecord->delete();
  
  ```
  
  But this is also timing out. Mike Pepper pointed me in the direction of the `a&m command` add-on at <https://github.com/am-impact/amcommand> but this is also struggling. Is the only way to deal with such large quantities of entries to do this directly via SQL?
  
  **Tags:** plugin-development, entrymodel
  
  > **Comments:**
  > **username_0**: Okay - been round circles and several different methods to achieve this. Unfortunately with this amount of entries, working directly with the craft model or API just timed out. To clean out the database I had to manually clear out entries from the DB via the following code: https://gist.github.com/cole007/c1c20774510d6a6b5ae5  Note that you will need to execute the `DELETE FROM craft_entries` query last  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2016-01-14. Score: 8 | Accepted answer
  
  Records are definitely not the way to go with dealing with large amounts of data sets in the database. There is just too much overhead associated with them.
  
  Manually running SQL (as you've discovered) is an option, but if you wanted to do this from PHP, you could still use `QueryBuilder`.
  
  ```php
  $elementIds = craft()->db->createCommand()
              ->select('id')
              ->from('entries')
              ->where('sectionId = :sectionId', array(':sectionId' => 18))
              ->queryColumn();
  
  $rowsAffected = craft()->db->createCommand()
              ->delete(
                  'elements',
                  array('in', 'id', $elementIds)
               );
  
  ```
  
  The MySQL cascading deletes setup on the foreign keys should take care of cleaning up the necessary rows in the remaining tables automatically.
  
  > **Comments:**
  > **username_0**: Hi username_2 - sorry for delay in following this up. Is there a typo in your code sample on the `delete()` method?  Seems to be throwing errors here.  
  > **username_3**: @username_0 it should have a comma after 'elements' and before the `array()` part. If that works edit the answer as such.  
  > **username_0**: Thanks @username_3 - caught that one but couldn't for the life of me see why it wasn't working. Turned out to be a typo - was using createComment rather than createCommand :D  
  
  ---
  
  ### Answer by username_1 on 2020-01-29. Score: 1
  
  I updated the queries @username_0 posted. This should work with Craft 3.x:
  
  ```
  DELETE elements FROM ut_entries AS entries, ut_elements_sites AS elements WHERE entries.id = elements.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE relations FROM ut_entries AS entries, ut_relations AS relations WHERE entries.id = relations.sourceId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE revisions FROM ut_entries AS entries, ut_revisions AS revisions WHERE entries.id = revisions.sourceId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE content FROM ut_entries AS entries, ut_content AS content WHERE entries.id = content.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE drafts FROM ut_entries AS entries, ut_drafts AS drafts WHERE entries.id = drafts.sourceId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE search FROM ut_entries AS entries, ut_searchindex AS search WHERE entries.id = search.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE elements FROM ut_entries AS entries, ut_structureelements AS elements WHERE entries.id = elements.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE attributes FROM ut_entries AS entries, ut_changedattributes AS attributes WHERE entries.id = attributes.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE fields FROM ut_entries AS entries, ut_changedfields AS fields WHERE entries.id = fields.elementId AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE relations FROM ut_entries AS entries, ut_relations AS relations WHERE (entries.id = relations.sourceId or entries.id = relations.targetId) AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  DELETE elements FROM ut_entries AS entries, ut_elements AS elements WHERE entries.id = elements.id AND entries.sectionId IN(1, 2, 3, 8, 9, 10, 11);
  
  ```
  
  
  ---
  
  