DELETE for deleting content entities

Last updated on
11 March 2023

Follow these steps to expose resources to DELETE requests:

Configuration

This builds upon the GET, POST and PATCH examples of the previous pages.

See Getting started: REST configuration & REST request fundamentals — Configuration

Sample requests below assume this configuration:

resources:
  entity:node:
    GET:
      supported_formats:
        - hal_json
      supported_auth:
        - basic_auth
        - cookie
    POST:
      supported_formats:
        - hal_json
      supported_auth:
        - basic_auth
        - cookie
    PATCH:
      supported_formats:
        - hal_json
      supported_auth:
        - basic_auth
        - cookie
    DELETE:
      supported_formats:
        - hal_json
      supported_auth:
        - basic_auth
        - cookie

Test with a DELETE request

Note that in this case, the Content-Type header is not necessary (because no request body is sent, so no data is sent, so there is nothing to have a MIME type).

cURL (command line)

curl --include \
  --request DELETE \
  --user klausi:secret \
  --header 'X-CSRF-Token: <obtained from http://example.com/rest/session/token>' \
  http://example.com/node/56?_format=hal_json \

Guzzle

<?php
$response = \Drupal::httpClient()
  ->delete('http://example.com/node/56?_format=hal_json', [
    'auth' => ['klausi', 'secret'],
    'headers' => [
      'X-CSRF-Token' => <obtained from /rest/session/token>
    ],
  ]);
?>

jQuery

function getCsrfToken(callback) {
  jQuery
    .get(Drupal.url('rest/session/token'))
    .done(function (data) {
      var csrfToken = data;
      callback(csrfToken);
    });
}

function deleteNode(csrfToken) {
  jQuery.ajax({
    url: 'http://example.com/node/56?_format=hal_json',
    method: 'DELETE',
    headers: {
      'X-CSRF-Token': csrfToken
    },
    success: function () {
      console.log('Node deleted.');
    })
  });
}

getCsrfToken(function (csrfToken) {
  deleteNode(csrfToken);
});

Help improve this page

Page status: No known problems

You can: