Deleting All Items from a Collection

I have the importing into a collection working like a treat. Thanks guys. But the thing I need to get sorted is the deleting of all records from a collection. I keep getting code errors. Can somebody give me the exact code for doing this?

Let’s say I have a page with an Input Box (#collectionInput) for loading the name of the collection I want to delete all records from.

The only other things is a Button (#deleteButton) which when pressed wipes the collection of all records.

Is this possible? Do I need to add anything else to the page?

Any chance of listing the code to be added for this to work?

1 Like

Hi Hugh,
There are several ways to achieve that. Here’s one of them:

First of all, add a dataset to the page (let’s say it’s name is ‘dataset1’), and set the permission to be Read & Write.
Then, create a button (let’s say ‘deleteRecordsButton’) with the following onClick() code:

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 also achieve it by using only ‘wix-dataset’ API.

Hope this helps,
Liran.

Unfortunately when I use this script, I just get the error Error: The current user does not have permissions to remove on the MyCollection collection. I have read-write settings on the page, proper permissions on the collection, and have the page hosting the script set as a site users page with login, but nothing but errors.