Swift
array manipulation
delete elements
programming
Swift array methods

Swift delete all array elements

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In Swift, the normal way to remove every element from an array is removeAll(). You can also assign a new empty array with [], but removeAll() communicates intent more clearly and gives you an extra option to keep the existing capacity. Most of the time, the right answer is simple; the only real question is whether you want to preserve allocated storage for future reuse.

The Standard Solution: removeAll()

The most direct API is:

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

Output:

text
[]

This empties the array in place. If other code refers to the same variable later, it still refers to the same array variable, now cleared.

Keeping Capacity

Swift also lets you preserve the array's allocated capacity.

swift
1var values = [10, 20, 30, 40]
2values.removeAll(keepingCapacity: true)
3print(values)
4print(values.capacity)

Keeping capacity can be useful when:

  • you repeatedly refill the array in performance-sensitive code
  • the array often grows back to roughly the same size
  • you want to reduce repeated allocations

If you do not care about capacity reuse, the plain removeAll() call is fine.

Assigning An Empty Array

Another valid pattern is simply reassigning the variable.

swift
var names = ["Ava", "Noah", "Liam"]
names = []
print(names)

This is concise and readable. The main tradeoff is semantic rather than functional:

  • 'removeAll() says "clear the existing collection"'
  • '[] says "replace this variable with a new empty literal"'

Both are acceptable in ordinary app code.

Do Not Remove Elements One By One

Beginners sometimes try to clear an array by repeatedly removing items in a loop.

swift
1var items = [1, 2, 3, 4]
2while !items.isEmpty {
3    items.removeLast()
4}

This works, but it is unnecessary and less expressive than removeAll(). If the goal is to empty the collection, use the collection API that already means exactly that.

Arrays Are Value Types

Swift arrays are value types with copy-on-write behavior. That matters when you think about who sees the cleared data.

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

Output:

text
[]
[1, 2, 3]

Clearing one array variable does not automatically clear every copy. That surprises developers coming from reference-type collection models.

Clearing Arrays Of Objects

If the array stores reference types such as class instances, clearing the array removes those references from the array. Whether the objects themselves are deallocated depends on whether other strong references still exist.

That means:

  • clearing the array empties the collection
  • object lifetime still depends on ARC and remaining references elsewhere

So removeAll() is not a magical "destroy every object everywhere" command. It only removes the array's ownership of those elements.

A Good Rule Of Thumb

Use these patterns intentionally:

  • 'array.removeAll() for the normal clear operation'
  • 'array.removeAll(keepingCapacity: true) when you expect to refill soon and care about reuse'
  • 'array = [] when replacement is clear and capacity control is irrelevant'

That is enough for almost every Swift codebase.

Common Pitfalls

  • Clearing an array element by element instead of using removeAll().
  • Forgetting about keepingCapacity when repeatedly rebuilding large arrays in tight loops.
  • Assuming clearing one array variable also clears other copied array values.
  • Expecting objects stored in the array to disappear if other strong references still exist.
  • Overthinking the choice between removeAll() and [] when the real code path is not performance-sensitive.

Summary

  • The standard way to delete all array elements in Swift is removeAll().
  • Use keepingCapacity: true if you want to reuse the array's storage.
  • Assigning [] is also valid and often readable.
  • Avoid manual loops when the goal is simply to clear the array.
  • Remember that Swift arrays are value types, so copies are independent after mutation.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.