Skip to content

Delete

Info

For simplicity, all the examples on this page, assume the HTTP client is already initialized and the requests are unauthenticated.

For an example of how to initialize the HTTP clients and make authenticated requests see authentication section

Deleting rows is performed by issuing DELETE requests.

Delete one row

Deleting one row is done by performing a DELETE request and providing the filtering condition on the primary key that uniquely identifies the row

curl -X DELETE \
http://localhost:8080/rest/todos?id=eq.10
await fetch(endpoint+'/rest/todos?id=eq.10', {method: 'DELETE'});
await client.delete('/rest/todos?id=eq.10');
await client.query({ 
    query: gql`
        mutation {
            delete {
                todo(id: 10) {
                    id
                }
            }
        }
    `
});

Deleting multiple rows

Deleting multiple rows is done by performing a DELETE request and providing the filtering condition

curl -X DELETE \
http://localhost:8080/rest/todos?id=gt.10
await fetch(endpoint+'/rest/todos?id=gt.10', {method: 'DELETE'});
await client.delete('/rest/todos?id=gt.10');
await client.query({ 
    query: gql`
        mutation {
            delete {
                todos(where: {id: {gt: 10}}) {
                    id
                }
            }
        }
    `
});

Receive the deleted row columns

In some cases, it's desired to receive in the response some columns of the deleted rows.

To achieve this result one must include the Prefer: return=representation header with the DELETE request. In almost all cases, this header is used in combination with the select query parameter to avoid fetching all the columns of the deleted row.

curl -X DELETE \
-H 'Prefer: return=representation' \
"http://localhost:8080/rest/todos?id=gt.10&select=id,created_at"
await fetch(endpoint+'/rest/todos?id=gt.10&select=id,created_at', {
    method: 'DELETE',
    headers: new Headers({'Prefer': 'return=representation'})}
});
const options = {headers: {'Prefer': 'return=representation'}}
await client.delete('/rest/todos?id=gt.10&select=id,created_at', {"todo":"new todo"}, options);
await client.query({ 
    query: gql`
        mutation {
            delete {
                todos(where: {id: {gt: 10}}) {
                    id
                }
            }
        }
    `
});

When deleting a single row, in order to receive back a json object representing the row (instead of a json array with a single item) include the Accept: application/vnd.pgrst.object+json header.

curl -X DELETE \
-H 'Prefer: return=representation' \
-H 'Accept: application/vnd.pgrst.object+json' \
"http://localhost:8080/rest/todos?id=eq.10&select=id,created_at"
await fetch(endpoint+'/rest/todos?id=eq.10&select=id,created_at', {
    method: 'DELETE',
    headers: new Headers({'Prefer': 'return=representation', 'Accept': 'application/vnd.pgrst.object+json'})}
});
const options = {headers: {'Prefer': 'return=representation', 'Accept': 'application/vnd.pgrst.object+json'}}
await client.delete('/rest/todos?select=id,created_at', options);
await client.query({ 
    query: gql`
        mutation {
            delete {
                todo(input: {todo: "new todo"}) {
                    id created_at
                }
            }
        }
    `
});