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:
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.
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:
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.
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.
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:
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.
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.

