Swift
array
delete elements
programming
coding

Swift delete all array elements

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Clearing an array in Swift is simple, but there are a few different ways to do it depending on whether you want to keep the array’s allocated capacity. The usual answer is removeAll(), but assigning an empty array literal can also be fine in some situations. The best choice depends on readability, performance, and whether the array will be filled again soon.

The Standard Way: removeAll()

The most direct way to delete every element is:

swift
1var numbers = [1, 2, 3, 4, 5]
2numbers.removeAll()
3
4print(numbers) // []

This keeps the same array variable and empties its contents.

For most code, this is the clearest option because it says exactly what you mean: remove every element.

Control Capacity with keepingCapacity

Swift lets you choose whether to preserve the array’s reserved storage.

swift
1var values = Array(0..<1000)
2values.removeAll(keepingCapacity: true)
3
4print(values.count) // 0

This is useful when:

  • the array was large
  • you expect to refill it soon
  • you want to avoid another allocation later

If the array is unlikely to be reused immediately, you can let Swift release storage more aggressively by not keeping capacity.

Assign an Empty Array

Another valid approach is assigning an empty literal:

swift
1var names = ["Ana", "Ben", "Cara"]
2names = []
3
4print(names)

This is simple and readable, but it does not communicate the capacity choice as explicitly as removeAll.

For most everyday code, both are acceptable. removeAll() is more descriptive when the intent is “clear the existing collection.”

Arrays Are Value Types

Swift arrays are value types with copy-on-write semantics. That means clearing one variable does not automatically clear another array that was copied from it.

swift
1var original = [1, 2, 3]
2var copy = original
3
4original.removeAll()
5
6print(original) // []
7print(copy)     // [1, 2, 3]

This matters when you expect reference-like behavior. The arrays can share storage internally until one is modified, but semantically they behave as separate values.

Use in Generic Code

If you are working with arrays inside helper functions, pass them as inout if the function should mutate the caller’s array.

swift
1func clearAll<T>(_ array: inout [T]) {
2    array.removeAll()
3}
4
5var words = ["one", "two", "three"]
6clearAll(&words)
7print(words)

Without inout, you would only clear a local copy.

Performance Considerations

For small arrays, the performance difference between removeAll() and array = [] is usually negligible. Capacity behavior matters more for large arrays or tight loops.

If you repeatedly reuse the same array:

swift
1var buffer = [Int]()
2buffer.reserveCapacity(10_000)
3
4for _ in 0..<100 {
5    buffer.append(contentsOf: 0..<5000)
6    buffer.removeAll(keepingCapacity: true)
7}

Keeping capacity can reduce repeated allocations in that kind of pattern.

Clearing Arrays of Reference Types

If the array contains class instances, removing all elements removes the references from the array. The actual objects are deallocated only if no other strong references remain.

swift
1final class Person {
2    let name: String
3    init(name: String) { self.name = name }
4}
5
6var people = [Person(name: "Ana"), Person(name: "Ben")]
7people.removeAll()

So clearing the array does not guarantee object destruction unless the array held the last strong references.

Common Pitfalls

The biggest mistake is forgetting that arrays are value types. Clearing one array variable does not clear a copied array elsewhere.

Another issue is ignoring keepingCapacity in performance-sensitive reuse loops. In most code it does not matter, but in buffer-style workloads it can.

Developers also sometimes write manual loops to remove elements one by one, which is noisier and less efficient than simply clearing the array directly.

Summary

  • Use removeAll() as the standard way to clear a Swift array.
  • Use removeAll(keepingCapacity: true) when the array will be refilled soon.
  • Assigning [] is also valid, but it is less explicit about capacity behavior.
  • Remember that arrays are value types with copy-on-write semantics.
  • Clearing an array of class instances removes references, not necessarily the objects themselves if other references still exist.

Course illustration
Course illustration

All Rights Reserved.