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

DOCSLANG


The DELETE statement.

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

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 simple expression:

// (a): SQL syntax
const result = await client.query(
    `DELETE FROM public.users
    WHERE role IS NOT NULL AND COALESCE(email, phone) IS NOT NULL)`
);
// (b): Object-based syntax
const result = await client.database('public').table('users').delete(
    { where: [
        { isNotNull: 'role' },
        { isNotNull: { fn: ['COALESCE', 'email', 'phone'] } }
    ] }
);
// (c): Function-based syntax
const result = await client.database('public').table('users').delete(
    { where: [
        { isNotNull: 'role' },
        { isNotNull: { fn: ['COALESCE', 'email', 'phone'] } }
    ] }
);

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')
            )
        )
    ] }
);
Clone this wiki locally