Search.../

truncate( )

Removes all items from a collection.

Description

The truncate() function returns a Promise that resolves after all items have been removed from the specified collection.

truncate() runs when at least one of the following is true:

  • The current user is the site owner.
  • The request is performed in backend code with a suppressAuth options value of true.

Calling the truncate() function does not trigger any hooks.

Note: truncate() also clears multiple-item reference fields in collections referenced by the specified collection. For example, suppose you have a Movies collection with an Actors field that contains multiple references to items in a People collection. Truncating the Movies collection also clears the data in the corresponding multiple-item reference field in the People collection.

Syntax

function truncate(collectionName: string, [options: WixDataOptions]): Promise<void>

truncate Parameters

NAME
TYPE
DESCRIPTION
collectionName

string

The collection to remove items from.

options
Optional

WixDataOptions

An object with the following boolean property: suppressAuth.

Returns

Fulfilled - When the items have been removed. Rejected - The error that caused the rejection.

Return Type:

Promise<void>

Was this helpful?

Site owner removes all items from a collection

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.truncate("myCollection")
6 .then( () => {
7 console.log("All items removed");
8 } )
9 .catch( (err) => {
10 let errorMsg = err;
11 } );
User who is not the site owner removes all items from a collection in the backend using data options

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let options = {
6 "suppressAuth": true
7};
8
9wixData.truncate("myCollection", options)
10 .then( () => {
11 console.log("All items removed");
12 } )
13 .catch( (err) => {
14 let errorMsg = err;
15 } );