Bulk delete a database

Hi. Trying to bulk remove data from a collection after finding the IDs.

Been scratching my head on this, and I’m sure its a small niggling issue.

Code as below


export function button2_click(event) {
    wixData.query("barlocations") //enter collection name here
    .limit(1000)
    .find()

.then( (results) => {
    if(results.items.length > 0) {
      let toDeleteArray = [];
      let items = results.items;
      let length = results.length;
      
      console.log(items);
      console.log(length);
      
            for (var i = 0; i < length; i++){ 
                let  toDelete =  { 
                                "ID":     items[i]._id
                                }; 
                    toDeleteArray.push(Object.assign({}, toDelete));
                     console.log("todelarray" + toDeleteArray[i].ID);
            }
            
            wixData.bulkRemove("barlocations", toDeleteArray[i].ID) //here is the problem
                .then( (results) => {
                console.log("removal done");
                 
            } )
            
        .catch( (error) => {
        let errorMsg = error.message;
        let code = error.code;
        });
        
  }
    });

}

At the wixData.bulkRemove part is the problem in console log which throws up the error Cannot read properties of undefined (reading ‘ID’)

Although the IDs are being registered in the todeletearray, for some reason, it cannot read it?

Hope for anyone’s guidance.

TIA.

I believe that instead of this:

wixData.bulkRemove("barlocations",toDeleteArray[i].ID)//here is the problem

You want this:

wixData.bulkRemove("barlocations",toDeleteArray)

The bulkRemove function wants an array, which is exactly what you built in the for loop.

Your code statement is only passing one item of the array: toDeleteArray[i].ID. Plus, the variable i has a value of the size of the array + 1. Therefore, the one item of the array that you are passing doesn’t even exist.

Thanks for replying.

Yes. I thought as much and was trying the same initially too.

But this error comes when I add the array → " Argument of type ‘{ ID: any; }[]’ is not assignable to parameter of type ‘string[]’. Type ‘{ ID: any; }’ is not assignable to type ‘string’

@jaoshsethna Ah, didn’t notice that you are building an array of objects. All you need is an array of strings. Something like this should work:

for (let i = 0; i < length; i++) {
    toDeleteArray.push(items[i]._id);
}

The code is untested, but I’m sure you get the idea. An array of strings, and the strings are the IDs to be deleted.

Yes. This worked thanks!

In the mean time, I came to know of wix truncate since I wanted to delete everything anyways! :smiley:

Hi folks,

Sorry to piggy back onto this post. It’s relevant, I promise. :blush:

So I have been using the same method as above to remove multiple items at once from a repeater, all working great.

What I’m trying to work out is how to remove one of the items from the array if I’ve selected it by mistake. To add some meat to the bones, I have a repeater with multiple dates shown. Each item has a checkbox to allow it to be selected, then a sperate button to delete them in bulk. All working well.

If I select an item and then realise that I don’t want to delete it then the work around I’m using it to clear the array and set the checkbox’s to false and then start again.
It works but it’s annoying me that there must be a better way. I’ve used pop which obviously works if its the last entry but ideally it would work that if the checkbox was unchecked it would remove that particular item from the array.

I’ve tried to use indexOf but been struggling as I need to set ids and then try to remove them for the bulkRemove again.

I hope this makes sense and if anyone had any documentation or point me in the right direction that would be ace.

$w('#lmtdDtsRptr').onItemReady(($item, itemData) => {

        $item('#bulkCckBox').onChange((event) => {
            
            if ($item('#bulkCckBox').checked) {
                toRemove.push(itemData._id)
            } else {
                toRemove = []
                $w('#bulkCckBox').checked = false;
            }
        });

    });

    $w('#rmvBtn').onClick(() => {
        wixData.bulkRemove('Busydates', toRemove).then((results) => { success(), limitedDates(); })
            .catch((err) => { error(err) });

Thank you!

I think indexOf could already be your solution.

What does mean → indexOf() ?

It means you get the INDEX-NUMBER of a specific VALUE inside of your ARRAY.

So let’s make an example:

Here your ARRAY with some VALUES inside.

const array = ["banana", "apple", "cherry", "monkey", "car", "whatever"];

let res1 = myArray.indexOf("banana");	console.log(res1);
let res2 = myArray.indexOf("apple");		console.log(res2);	
let res3 = myArray.indexOf("monkey");	console.log(res3);
let res4 = myArray.indexOf("car");		console.log(res4);
let res5= myArray.indexOf("whatever");	console.log(res5);
const myArray = ["banana", "apple", "cherry", "monkey", "car", "whatever"];

console.log(myArray);

const index = myArray.indexOf("cherry");
if (index > -1) { // only splice array when item is found
  myArray.splice(index, 1); // 2nd parameter means remove one item only
}

RESULT should be…

[banana, apple, monkey, car whatever];

So in this example you get theINDEX of the found VALUE of your ARRAY, to be able to remove it from your ARRAY.

So if you uncheck —> you start the splice function to delete selected value from your array.

And if you check a checkboxvalue → you generate a function which would add the selected value to your already existing array → for example → myArray.push(…).

This would regulate all values inside of your array.

And once you click onto DELETE-BUTTON —> this would delete all items inside of your DB, which are included inside of your ARRAY at this moment and immediately would clear the ARRAY at least.

@russian-dima Thanks so much for taking the time to show this in detail. I was making it way to complex when there was a much easier solution with indexOf.

Thanks again!