Unuable to delete collection records through a script.

We have a data set that has grown to 15000 records and has become very difficult to manage. The data set needs re-imported weekly in entirety, so we have to clear out the date set first.

First off WIX REALLY REALLY needs a delete all records function for a collection or an option to replace all on import. This would save countless hours of aggrevation!

So previously, I have been going to manage the collection and using a UI trick to delete 1000 records at a time. Not sure what changed, but this no longer works as it just times out.

I found a number of posts offering scripts to delete all records, so I implemented a recursive version of those since these other scripts can only delete 1000 records at a time. But whenever I run this, I get the following error.

Uncaught (in promise) Error: The current user does not have permissions to remove on the AllStateMasterAdditionalFilter collection.

I have the collection on the page set to read-write, have the permissions set on the collection for delete to Admin, and have the page set up as a Members only page. I have tried logged in as Admin and Site Owner with the same results. Nothing gets deleted.


import wixData from 'wix-data';

$w.onReady(function () {
});

export function DeleteMaster_click(event) {
	console.log("Deleting all records");
	deleteDataRecursive("AllStateMasterAdditionalFilter", 0);
}

function deleteDataRecursive (collectionName, recordCount) {
	wixData.query(collectionName)
		.limit(100)
		.find()
		.then((result) => {
			var pageRecordCount = result.items.length;
			console.log('Deleting ' + pageRecordCount + ' records');
			var recordsProcessed = 0;
			for (var i = 0; i < result.items.length; i++){
				if (result.items[i] === null) continue;
				var item = result.items[i];
				wixData.remove(collectionName, item._id)
					.then((removeResult) => {
						recordsProcessed ++;
						if ((recordCount + recordsProcessed) % 10 === 0) {
							console.log('Deleted record count: ' + (recordCount + recordsProcessed));
						}
					})
			}
			recordCount += pageRecordCount;
			if (pageRecordCount > 0) {
				deleteDataRecursive(collectionName, recordCount);
			}
			else {
				console.log('delete data done.' + recordCount + ' records removed.' );
			}
		})
		.catch((err) => 
		{
			console.log ('query error:' + err);
			deleteDataRecursive(collectionName, recordCount);
		});
}

Hi,
I’d recommend you to use the following code to delete the record:

export function deleteRecordsButton_click(event) {
	const collectionName = $w('#collectionInput');
	const numberOfItems = $w('#dataset1').getTotalCount();
	$w('#dataset1').getItems(0, numberOfItems).then(results => {
		const items = results.items;
		items.forEach(item => {
			wixData.remove(collectionName, item._id);
		});
	});
}

You can loop on the function until you don’t have record in your collection.
If the problem persists, please share your editor’s URL so we can inspect.

I’ve implemented that code in my project and it only is deleting one item at a time. So I’ve tried adding a loop to it and still no luck. Any thoughts?

export function deleteRecordsButton_click(event) {
for ( var i = 0; i <= 5; i++){

const collectionName = $w(‘#dataset1’);
const numberOfItems = $w(‘#dataset1’).getTotalCount();
$w(‘#dataset1’).getItems(0, numberOfItems).then(results => {
const items = results.items;
items.forEach(item => {
wixData.remove(collectionName, item._id);
});
});
}
}

If you’re trying to delete all records in a collection then the information in this thread is out of date: Wix added a function that does exactly that.

https://www.wix.com/corvid/reference/wix-data.html#truncate

Exactly, you are much better off adding a brand new forum post with your own question instead of commenting on one that is a year old now.

Like Lee has mentioned already a lot of old posts will be no good to just copy the example code used as Wix may have changed it or have added new ways etc.