<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>

CC BY-SA 4.0
2020-01-29T15:23:19.843Z
2020-01-29T15:23:19.843Z
Harald Atteneder
<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>

CC BY-SA 3.0
2016-02-03T10:33:52.293Z
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.
Cole Henley
CC BY-SA 3.0
2016-02-03T11:10:47.743Z
@ColeHenley it should have a comma after 'elements' and before the `array()` part. If that works edit the answer as such.
Josh Angell
CC BY-SA 3.0
2016-02-03T11:28:23.153Z
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
Cole Henley
CC BY-SA 4.0
2016-01-14T18:33:40.827Z
2023-03-06T21:11:56.757Z
2023-03-06T21:11:56.757Z
Brad Bell
<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>

CC BY-SA 3.0
2016-01-14T13:13:50.513Z
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
Cole Henley
<plugin-development><entrymodel>
Deleting all entries from a Section
# 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.
$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 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.  
> **username_3**: @ColeHenley it should have a comma after 'elements' and before the `array()` part. If that works edit the answer as such.  
> **username_0**: 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  

---

### Answer by username_1 on 2020-01-29. Score: 1

I updated the queries @ColeHenley 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);


---

