Skip to content

noArrayDelete (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noArrayDelete": "error"
}
}
}
}

Disallow using the delete operator on array elements.

The delete operator removes a property from an object. When you use it on an array element, the element is removed, but the array doesn’t get shorter and the elements after it don’t move. The array is left with an empty slot, also called a “hole”, at that position:

const array = [1, 2, 3];
delete array[1];
console.log(array); // [1, <1 empty item>, 3]
console.log(array.length); // 3

Array methods treat holes in different ways. For example, forEach() skips them, while for...of loops and includes() treat them as undefined. To remove elements from an array, use the splice() method instead.

The rule needs type information to know whether a value is an array, so it only reports values that are known to be arrays or tuples.

The fix replaces delete array[index] with array.splice(index, 1). It’s unsafe, because splice() moves the elements after the removed one to lower positions, and it returns the removed elements instead of true. The fix isn’t offered when the expression contains comments, or when the code doesn’t use semicolons and removing delete would join the statement to the previous line.

invalid-array.ts
declare const array: number[];
delete array[0];
/invalid-array.ts:2:1 lint/nursery/noArrayDelete  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ Avoid using the delete operator on array elements.

1 │ declare const array: number[];
> 2 │ delete array[0];
│ ^^^^^^^^^^^^^^^
3 │

ℹ Deleting an array element leaves an empty slot in the array and doesn't change its length.

ℹ Use splice() to remove the element instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use splice() instead.

1 1 │ declare const array: number[];
2 │ - delete·array[0];
2 │ + array.splice(0,·1);
3 3 │

invalid-tuple.ts
declare const tuple: [string, number];
delete tuple[1];
/invalid-tuple.ts:2:1 lint/nursery/noArrayDelete  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ Avoid using the delete operator on array elements.

1 │ declare const tuple: [string, number];
> 2 │ delete tuple[1];
│ ^^^^^^^^^^^^^^^
3 │

ℹ Deleting an array element leaves an empty slot in the array and doesn't change its length.

ℹ Use splice() to remove the element instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use splice() instead.

1 1 │ declare const tuple: [string, number];
2 │ - delete·tuple[1];
2 │ + tuple.splice(1,·1);
3 3 │

valid.ts
declare const array: number[];
array.splice(0, 1);
declare const object: Record<string, number>;
delete object["key"];
  • To report delete on the properties of any object, without type information, see noDelete. That rule avoids delete because it can make code slower. It doesn’t report keys that are variables, such as delete array[index], and its fix assigns undefined, which leaves the array length unchanged. Both rules report delete array[0].