Back to top

ReQL command: delete_at

Command syntax

array.delete_at(offset [,end_offset]) → array

Description

Remove one or more elements from an array at a given index. Returns the modified array. (Note: delete_at operates on arrays, not documents; to delete documents, see the delete command.)

If only offset is specified, delete_at removes the element at that index. If both offset and end_offset are specified, delete_at removes the range of elements between offset and end_offset, inclusive of offset but not inclusive of end_offset.

If end_offset is specified, it must not be less than offset. Both offset and end_offset must be within the array’s bounds (i.e., if the array has 10 elements, an offset or end_offset of 10 or higher is invalid).

By using a negative offset you can delete from the end of the array. -1 is the last element in the array, -2 is the second-to-last element, and so on. You may specify a negative end_offset, although just as with a positive value, this will not be inclusive. The range (2,-1) specifies the third element through the next-to-last element.

Example: Delete the second element of an array.

> r.expr(['a','b','c','d','e','f']).delete_at(1).run(conn)

['a', 'c', 'd', 'e', 'f']

Example: Delete the second and third elements of an array.

> r.expr(['a','b','c','d','e','f']).delete_at(1,3).run(conn)

['a', 'd', 'e', 'f']

Example: Delete the next-to-last element of an array.

> r.expr(['a','b','c','d','e','f']).delete_at(-2).run(conn)

['a', 'b', 'c', 'd', 'f']

Example: Delete a comment on a post.

Given a post document such as:

{
    id: '4cf47834-b6f9-438f-9dec-74087e84eb63',
    title: 'Post title',
    author: 'Bob',
    comments: [
        { author: 'Agatha', text: 'Comment 1' },
        { author: 'Fred', text: 'Comment 2' }
    ]
}

The second comment can be deleted by using update and delete_at together.

r.table('posts').get('4cf47834-b6f9-438f-9dec-74087e84eb63').update{ |post|
    { :comments => post['comments'].delete_at(1) }
}.run(conn)

Get more help

Couldn't find what you were looking for?