All Row Delete in Collections

Hi,

I been using this query for deleting all records in the collection. Its working fine. But not all in one extent. After sometime its getting stopped and when we press the button its going one by one

import wixData from ‘wix-data’;

$w.onReady( function () {
setTimeout(() => {
$w(“#table1”).refresh();
}, 3000);
});
export function outstanding_click(event, $w) {

//Add your code for this event here:
$w(“#delete”).onReady( async () => {
if ($w(“#delete”).getCurrentItem()) {
await $w(“#delete”).remove();
while ($w(“#delete”).hasNext()) {
await $w(“#delete”).next();
await $w(“#delete”).remove()
}
}
});
}

Please help me

1 Like

You can use one of these two functions to delete all records.

Using a database query:

export function deleteAll_onClick(event, $w) {
    wixData.query("NewCollectionName”)
    .limit(1000)
    .find()
    .then((result) => {
        for (var i = 0; i < result.items.length; i++){
            if (result.items[i] === null) continue;
            wixData.remove("NewCollectionName", result.items[i]._id);
        }
        console.log('erase done');
    });
}

Using a dataset:

export function deleteAll_onClick(event, $w) {
    let dataset = $w("#dynamicDataset"); // or the name of your dataset
    dataset.onReady(async() => {
        while (dataset.getCurrentItem()) {
            await dataset.remove();
        }
    });
    dataset.refresh();
}

I hope this helps,

Yisrael

Excellent Sir. Thanks Let me try

Working Perfectly. Thanks a million

Dear YIsrael

I need another favor, which I have already raised but no reply yet. Need you help on the same.

I am trying to do a manual upload in Json form when I upload its perfectly working when it come to Date field am getting stuck.

What I did was made a hooks beforeInsert from UTC date format to Javascript Date Object. This is the code

export function LM_Attendance_beforeInsert(item, context) {
let hookContext = context; // see below

var str = item.at_date;
var dt = new Date(str + “Z”);
let at_date_date = dt.getDate();
let at_date_month = dt.getMonth()+1;
let at_date_year = dt.getFullyear();
let at_date_hours = dt.getHours();
let at_date_minutes = dt.getMinutes();
let at_date_sec = dt.getSeconds();
let at_date_full = new Date(at_date_year, at_date_month, at_date_date, at_date_hours, at_date_minutes, at_date_sec, 0)
item.at_date = at_date_full
return item;
}

Its not working.

Date is coming but the time is showing as 5 30 am in the database. Am I doing the right thing.

Please advise and help me out

Hey Continental,

Do you know what in fact is being received by the hook? Is the time part of the date? Where is the 5:30 am coming from.

Yep. Its coming as UTC Date format in JSON Please find below JSon File. Date is coming perfectly. Time is a concern.

[
{
“at_email”: “sdfg dgf”, // Email masked
“at_date”: “2018-06-27T00:00:00.00”,
“at_in”: “2018-06-27T08:42:00.00”,
“at_out”: “2018-06-27T14:42:00.00”,
“at_actual”: “2018-06-27T00:00:00.00”,
“at_regtime”: “2018-06-27T00:00:00.00”,
“at_regfullday”: “2018-06-27T00:00:00.00”,
“at_reg_done”: 0,
“at_mobile”: “Biometric”
},
{
“at_email”: “s dgdfgdfgsd”, // Email masked
“at_date”: “2018-06-26T00:00:00.00”,
“at_in”: “2018-06-26T08:42:00.00”,
“at_out”: “2018-06-26T14:00:00.00”,
“at_actual”: “2018-06-26T00:00:00.00”,
“at_regtime”: “2018-06-26T00:00:00.00”,
“at_regfullday”: “2018-06-26T00:00:00.00”,
“at_reg_done”: 0,
“at_mobile”: “Biometric”
}
]
Can you also advise whether the above code in the previous publish is right???

Any update Chief

The hook code appears to be right. Do you know that at_date_full is correct? I really don’t understand why the time isn’t being saved correctly.

Its right YIsrael. Not too sure… Can you escalate to the developers. What do you suggest/advise?. Its very for my project. Please help me out

Hi Yisrael,
Can you please help me.
I have 10 collections, each collection has near 8000 items. Every couple of weeks I need to delete all the items (Rows )in each collection ( including Live) and import new items.

I spend a very long time highlighting rows and clicking the delete button. I am absolutely exhausted when it all deleted.

I notice above you have given two codes
1)Using a database query
2) Using a dataset

I tried No 2) But the items in the collections are not deleting.

This is what I did:-
1)Opened a New page
2) Added a Dataset
3) connected the Dataset to Dresses
4) Added the code that you have given in “No. 2 Using a datase”

But when I go to the Dresses Collection all the items are there.
Please tell me what I am doing wrong here.

How can I delete all the rows on the Dresses Live well

Really appreciate your advice.

I have a very little knowledge about coding, so can you please write things down step by step.
Thanks.

can some one please help me!

@Dilly,

Do you get any messages in the Developers console?

Hi Yisrael,
Thank you for your reply. No messages. I am not sure if I am pasting the code in the correct area or page.

When you get some time can you kindly write down step by step what I should be doing. I am totally new to coding. Thank you so much.

Please post the URL of your site. Only authorized Wix personnel can get access to your site in the editor.

Sure. https://www.stylesweetsmile.com

Under the database the last collection name is “mypage” you can use this to delete rows if you like. I do not need the items in this collection.

I have to delete rows in every collection every three weeks then import new items. this is why i am looking for a way to delete rows.

What page do you have the code?

Dilly,

I added a test page to your site, put a button on the page, added a dataset connected to the mypage collection, and then used this code:

export function deleteAll_click(event, $w) {
   let dataset = $w("#dataset1"); // or the name of your dataset
   dataset.onReady(async() => {
      while (dataset.getCurrentItem()) {
         console.log('x');
         await dataset.remove();
      }
   });
   dataset.refresh();
}

It works fine.

You can also use this:

export function deleteAll_onClick(event, $w) {
    wixData.query("mypage”)
    .limit(1000)
    .find()
    .then((result) => {
       for (var i = 0; i < result.items.length; i++) {
          if (result.items[i] === null) continue;
          wixData.remove("mypage", result.items[i]._id);
       }
       console.log('erase done');
    });
}

Good luck,

Yisrael

I am really confused Yisrael. It is still not working. I can still see all the items (rows) in the mypage collection.

This is what I did.

  1. I clicked on Menues & Pages
  2. Added a page ( page name mypage)
  3. Added a dataset - connected to mypage collection ( I didn’t change any settings in the dataset)
  4. Added a button
  5. Clicked on onclick
  6. added the above first code you gave me ( below the export function)
  7. Clicked preview
  8. clicked on Button
  9. went back to the collection on editor

I can still see all the items in the collection.