# All Row Delete in Collections

**URL:** <https://forum.wixstudio.com/t/all-row-delete-in-collections/2128>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [August 15, 2018, 8:07am UTC](https://forum.wixstudio.com/t/all-row-delete-in-collections/2128 "2018-08-15T08:07:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oalbps](https://avatars.discourse-cdn.com/v4/letter/o/22d042/32.png) [@oalbps](https://forum.wixstudio.com/u/oalbps)\
**Post date:** [August 15, 2018, 8:07am UTC](https://forum.wixstudio.com/t/all-row-delete-in-collections/2128/1 "2018-08-15T08:07:06Z")

</div>

Hi All,

We’ve modified the all row delete code provided in [https://www.wix.com/code/home/forum/community-discussion/all-row-delete-in-collections](https://www.wix.com/code/home/forum/community-discussion/all-row-delete-in-collections)

New code as follows (triggered after clicking button5);

**export async function** button5\_click(event, $w) {  
//Add your code for this event here:  
var j;  
var mygrp = 1000;  
var totitems = **await** wixData.query(“CntrRetCollection”).count();  
var noofloops = Math.ceil(totitems / mygrp);

**for** (j = 1; j\<=noofloops; j++) {  
const result = **await** wixData.query(“CntrRetCollection”).limit(1000).find();  
// the next line will run only after the query has finished  
**for** ( var i = 0; i \< result.items.length; i++){  
if (result.items[i] === **null** ) **continue** ;  
**await** wixData.remove(“CntrRetCollection”, result.items[i].\_id);  
}  
console.log(“Delete complete”);  
}  
}

Roughly half of the time - all 3000+ items are deleted without issue.

The rest of the time - deletion just stops at some random point i.e. sometimes only a few hundred items are deleted and other times only a few items remain and deletion process just stops.

Just wondering if anyone has any idea what may be causing this issue?

Thanks.

---

<div class="post-metadata">

**Author:** ![roi-bendet](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/roi-bendet/32/1283_2.png) [@roi-bendet](https://forum.wixstudio.com/u/roi-bendet)\
**Post date:** [August 15, 2018, 8:23am UTC](https://forum.wixstudio.com/t/all-row-delete-in-collections/2128/2 "2018-08-15T08:23:27Z")

</div>

Hi,  
Check out this function:

```javascript
export function cleanButtonOnClick(event, $w) {
    $w("#dynamicDataset").onReady(async() => {
    if ($w("#dynamicDataset").getCurrentItem()) {
    await $w("#dynamicDataset").remove();
    while ($w("#dynamicDataset").hasNext()) {
        await $w("#dynamicDataset").next();
        await $w("#dynamicDataset").remove();
            }
        }
    });
}

```

Just connect the dataset to the wanted collection.  
Roi.

---

<div class="post-metadata">

**Author:** ![oalbps](https://avatars.discourse-cdn.com/v4/letter/o/22d042/32.png) [@oalbps](https://forum.wixstudio.com/u/oalbps)\
**Post date:** [August 15, 2018, 8:37am UTC](https://forum.wixstudio.com/t/all-row-delete-in-collections/2128/3 "2018-08-15T08:37:04Z")

</div>

Hi Roi - thanks for the suggestion. Actually the code works but only roughly half of the time. We’ve also tried Yisrael’s suggested database query method verbatim from [https://www.wix.com/code/home/forum/community-discussion/all-row-delete-in-collections](https://www.wix.com/code/home/forum/community-discussion/all-row-delete-in-collections) with same result so wondering if cause could be something else rather than a coding issue.

---

<div class="post-metadata">

**Author:** ![gd\_law](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gd_law/32/84016_2.png) [@gd\_law](https://forum.wixstudio.com/u/gd_law)\
**Post date:** [June 28, 2019, 5:05am UTC](https://forum.wixstudio.com/t/all-row-delete-in-collections/2128/4 "2019-06-28T05:05:28Z")

</div>

Hi Oalbps and Roi,

I have the very same issue. When I try to remove even 90 records the routine will leave about 5-10 records that should have been removed. I also used the function that Yisrael posted. I have tried running it in the backend as well as in page code and there is more consistency from the backend than from page code, but, still inconsistent for real world use.

This is a major issue!! The integrity of the data will be compromised greatly if these random results keep happening. have you guys had any luck understanding what’s going on here?
