Dropping content of a Wix Collection.

Is there a quick and easy way to remove all entries (using code!) from a Wix Collection?

Try one of these two methods:

let dataset = $w("#dataset"); // or the name of your dataset
   dataset.onReady(async() => {
   while (dataset.getCurrentItem()) {
      await dataset.remove();
   }
});
dataset.refresh();
wixData.query("collection”)
   .limit(1000) // put whatever limit works for you here
   .find()
   .then((result) => {
      for (var i = 0; i < result.items.length; i++){
         if (result.items[i] === null) continue;
         wixData.remove("collection", result.items[i]._id);
      }
      console.log('erase done');
   });

Yisrael

So I gather there’s no direct option to drop all records (i.e. I have to iterate through all records). Oh well…
Thanks.

Hey Yisrael.
What is this line for?
wixData.remove(“collection”, result.items[i]._id);

Hi Tal,

The remove() function removes a row, based on its id , from the collection.

Yisrael

Hey. Sorry. I accidentally pasted the wrong line. I meant this one:
if (result.items[i] === null) continue;

It’s really a sanity check to see if the indexed item exists. I suppose this would make more sense:
if (result.items[i] === null) break;
That would jump out of loop on the assumption that if an indexed item doesn’t exist, then we’ve reached the end and there are no more rows to remove. Guess it all depends on if we can assume that the assumption is valid.

Is it even possible for wixData.query("collection”).find() to have non existent items in its results?

The existence of a non-existent item sounds like an oxymoron, but I suppose that the row could be undefined, depending on the contents of that row in the the collection. To me it sounds like a bug, but who knows. I’ll ask some of our smart people and see what they say.

And even if that does happen, doesn’t it just mean that the remove() function will return a promise that will be rejected? I mean - will there actually be any problem with running remove() on a non-existent item?

Ya know something, now I’m not so sure I like that code. The remove() doesn’t even have a .then() . Althought, maybe it doesn’t need it, it just tries to remove the row and moves on. I believe that I’ve used this code before to clean up one of my own collections. I’ll have to look into this. Seems fishy to me. Thanks.

Cool. So I’ll be waiting to hear from you. Also, while you’re at it - while I don’t think I’d have more than 1000 items in my collection, I guess I should have something in place in case there are more than this. What would be the most elegant way of doing that? (just set some flag that changes if and when there are no more items in the collection?)

Well, the remove() doesn’t need a .then() since we’re not really waiting for results. If we wanted to return the removed object, we would need something like this:

wixData.remove("collection", result.items[i]._id)
.then( (results) => {
    let item = results;
    // do something with returned row (item)
} )
.catch( (err) => {
    let errorMsg = err;
} );

In our case (stomping through the collection and removing everything in our path), we don’t need each removed item, and therefore there is no need for the .then() function. We just request a remove and move on.

The sanity check discussed above is a good idea since it ensures that our little snippet of code doesn’t crash and burn.

As far as removing more than a 1000 items, you can either bump up the limit, or just put this query in a loop which keeps going until the collection has no more items left.

OK. Thanks.

Two things that I just thought about:

  1. The way I thought I’d implement it is with a while loop that keeps going as long as the results.items.length != 0. However, seeing as remove() is asynchronous - it’s quite likely that the first iteration (let’s say deleting items 1 - 1000) will finish before remove() has actually finished deleting all the items - meaning, the next iteration’s find() will also find some not-yet-deleted-but-already-on-their-way-to-being-deleted items, and then try to remove them again. Is that a problem? Wouldn’t it make more sense, then, to add “await”, to make sure each item is actually deleted, before moving forward? (I’m guessing that would also mean the run time MUCH longer, wouldn’t it?)
  2. Can I trust that remove() actually succeeds in deleting the item, or is it possible for it to fail? (I don’t want to take the risk that there are still leftovers)

Hi Tal,

I think your points are well taken. My general philophy is “don’t trust anything”. So, if your use case is “critical”, then yes, I might opt for a more robust method of deleting the contents - even if it means significantly decreased perfomance.

However, I’ve never seen a problem with either of the methods that were posted. You can take that for what’s it worth. I personally would rely on them. Interesting to think about nonetheless.

Yisrael

Alright.
One more question: is the other option you suggested above (using a dataset) even possible when the whole thing is done entirely in the backend? That is - if I’m not mistaken, a dataset is an element on a page, which means I can’t access or define it in the backend. Right?

I believe you’re correct about the dataset. A dataset is a component that is not visible on the page. Since it’s a component, it needs to be used in the client side.

OK. Thanks.