Search.../

delete( )

A function that responds to requests made with the HTTP DELETE method.

Description

The HTTP DELETE method is usually called by consumers to delete an existing resource. If defined in this way and the resource is deleted, the function should respond with a 200 (OK) status code.

Respond to the request by returning a WixHttpFunctionResponse object you create using one of the response(), ok(), created(), notFound(), serverError(), badRequest(), or forbidden() functions.

The delete() function is not a function that you call from your code. You define the function in a file named http-functions.js in your site's Backend section. The function is called when your users make HTTP requests using the associated URLs as described below.

All DELETE requests with the following URL will be routed to this function:

Premium sites:

https://www.{user_domain}/_functions/<functionName>
Copy Code

Free sites:

https://{user_name}.wixsite.com/{site_name}/_functions/<functionName>
Copy Code

Notes:

  • The HTTP DELETE request can't contain a body and can't contain the header "Content-Type": "application/json". If the request breaks this syntax and includes either of these, the function returns a 400 (Bad Request) status code.
  • You must publish your site at least once before using both the testing and production endpoints. When you make changes to production endpoints you must publish your site for them to take effect. Testing endpoints will use the latest code in the editor.

Syntax

function delete(request: wixhttpfunctionrequest-obj): WixHttpFunctionResponse

delete Parameters

NAME
TYPE
DESCRIPTION
request

The request object.

Returns

Was this helpful?

Create a DELETE HTTP function

This example creates a DELETE HTTP function named myFunction.

Copy Code
1// In http-functions.js
2
3import {ok} from 'wix-http-functions';
4
5// URL looks like:
6// https://www.mysite.com/_functions/myFunction/someId
7// or:
8// https://user.wixsite.com/mysite/_functions/myFunction/someId
9export function delete_myFunction(request) {
10
11 const toRemove = request.path[0];
12
13 // remove the resource
14
15 return ok();
16}
17
Create a DELETE HTTP function

This example creates a DELETE HTTP function named myFunction that deletes an item from a collection named myCollection based on the path of the request. If the item is deleted successfully, an OK response is returned.

Copy Code
1// In http-functions.js
2
3import {ok, serverError} from 'wix-http-functions';
4import wixData from 'wix-data';
5
6// URL looks like:
7// https://www.mysite.com/_functions/myFunction/76bf-8bfa-714
8// or:
9// https://user.wixsite.com/mysite/_functions/myFunction/76bf-8bfa-714
10export function delete_myFunction(request) {
11 let options = {
12 "headers": {
13 "Content-Type": "application/json"
14 }
15 };
16 // delete the item from a collection
17 return wixData.remove("myCollection", request.path[0])
18 .then( (results) => {
19 options.body = {
20 "deleted": results
21 };
22 return ok(options);
23 } )
24 // something went wrong
25 .catch( (error) => {
26 options.body = {
27 "error": error
28 };
29 return serverError(options);
30 } );
31}
32