Array#delete_at

arr.delete_at(index) -> obj or nil
Returns: Object or nil · Updated March 13, 2026 · Array Methods
ruby array mutation

The delete_at method removes and returns the element at a specified index from an array. Unlike delete which removes by value, delete_at targets a specific position.

fruits = ["apple", "banana", "cherry", "date"]
fruits.delete_at(1)
# => "banana"
fruits
# => ["apple", "cherry", "date"]

Syntax

arr.delete_at(index)

Parameters

ParameterTypeDescription
indexIntegerThe zero-based position of the element to remove

Return Value

delete_at returns the removed element, or nil if the index is out of range:

arr = ["a", "b", "c"]

# Remove element at index 1
arr.delete_at(1)
# => "b"
arr
# => ["a", "c"]

# Index out of bounds returns nil
arr = [1, 2, 3]
arr.delete_at(99)
# => nil
arr
# => [1, 2, 3] (unchanged)

Basic Examples

Removing by Index

# First element
arr = [1, 2, 3]
arr.delete_at(0)
# => 1
arr
# => [2, 3]

# Last element
arr = [1, 2, 3]
arr.delete_at(-1)
# => 3
arr
# => [1, 2]

# Middle element
arr = ["a", "b", "c", "d"]
arr.delete_at(2)
# => "c"
arr
# => ["a", "b", "d"]

Using Negative Indices

Negative indices count from the end of the array:

arr = [10, 20, 30, 40, 50]

# Second-to-last element
arr.delete_at(-2)
# => 40
arr
# => [10, 20, 30, 50]

Common Use Cases

Priority Queue

# Process highest priority item first
queue = [["low", "task1"], ["high", "task2"], ["medium", "task3"]]

# Remove highest priority (index 0)
current = queue.delete_at(0)
current
# => ["low", "task2"]

Slot-based Game

# Remove item from a specific slot
inventory = ["sword", "shield", nil, "potion", "armor"]
inventory.delete_at(2)
# => nil
inventory
# => ["sword", "shield", "potion", "armor"]

Dynamic List Management

# Remove processed items
queue = ["task1", "task2", "task3"]
processed = queue.delete_at(0)
# => "task1"
# queue is now ["task2", "task3"]

Comparing with Other Methods

MethodSearches ByRemovesReturns
delete_atindexSingle elementDeleted element or nil
deletevalueAll matchesDeleted value or nil
sliceindex/rangeElementsDeleted portion or nil
pop-Last elementLast element or nil
shift-First elementFirst element or nil
arr = ["a", "b", "c", "d"]

# delete_at - remove by index
arr = ["a", "b", "c", "d"]
arr.delete_at(1)
# => "b"
arr
# => ["a", "c", "d"]

# delete - remove by value
arr = ["a", "b", "c", "d"]
arr.delete("b")
# => "b"
arr
# => ["a", "c", "d"]

Gotchas

Out of Bounds Returns Nil

Unlike some methods that raise an error, delete_at silently returns nil for invalid indices:

arr = [1, 2, 3]
arr.delete_at(100)
# => nil (no error!)
arr
# => [1, 2, 3]

Shifting Indices

After removing an element, all subsequent indices shift down:

arr = [0, 1, 2, 3, 4]

# Remove index 2
arr.delete_at(2)
# => 2
arr
# => [0, 1, 3, 4]

# What was at index 3 is now at index 2
arr[2]
# => 3

Mutates Original Array

delete_at modifies the array in place:

original = [1, 2, 3]
copy = original
original.delete_at(0)
# => 1
original
# => [2, 3]
copy
# => [2, 3] (also changed!)

See Also