Skip to content
Oxford Harrison edited this page Nov 19, 2024 · 7 revisions

DOCSLANG


The DELETE statement.

See APIS ➞ client.query(), table.delete()

Section Description
Basic Delete Run a basic DELETE operation.
The WHERE Clause -
The RETURNING Clause -

Basic Delete

// (a): SQL syntax
const result = await client.query(
    `DELETE FROM public.users`
);
// (b): Object-based syntax
const result = await client.database('public').table('users').delete();

The WHERE Clause

Find by simple expression:

// (a): SQL syntax
const result = await client.query(
    `DELETE FROM public.users
    WHERE name = 'John' AND role = 'guest'`
);
// (b): Object-based syntax
const result = await client.database('public').table('users').delete(
    { where: [
        { eq: ['name', { value: 'John' }] },
        { eq: ['role', { value: 'guest' }] }
    ] }
);
// (c): Function-based syntax
const result = await client.database('public').table('users').delete(
    { where: [
        (q) => q.eq('name', (r) => r.value('John')),
        (q) => q.eq('role', (r) => r.value('guest'))
    ] }
);

Find by complex expression:

// (a): SQL syntax
const result = await client.query(
    `DELETE FROM public.users
    WHERE (role = $1 OR role = $2) AND (
        email IS NOT NULL OR (
            phone IS NOT NULL AND country_code IS NOT NULL
        )
    )`
);
// (b): Object-based syntax
const result = await client.database('public').table('users').delete(
    { where: [
        { some: [
            { eq: ['role', { binding: 'admin' }] },
            { eq: ['role', { binding: 'contributor' }] }
        ] },
        { some: [
            { isNotNull: 'email' },
            { every: [
                { isNotNull: 'phone' },
                { isNotNull: 'country_code' }
            ] }
        ] }
    ] }
);
// (c): Function-based syntax
const result = await client.database('public').table('users').delete(
    { where: [
        (q) => q.some(
            (r) => r.eq('role', (s) => s.binding('admin')),
            (r) => r.eq('role', (s) => s.binding('contributor')),
        ),
        (q) => q.some(
            (r) => r.isNotNull('email'),
            (r) => r.every(
                (s) => s.isNotNull('phone'),
                (s) => s.isNotNull('country_code')
            )
        )
    ] }
);

The RETURNING Clause

Examples coming soon.

Clone this wiki locally