0Pricing
SwiftUI Academy · Lesson

Swipe-to-Delete Rows

Remove items with onDelete.

Swipe-to-Delete Rows is a free SwiftUI Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Lists That Can Lose Rows

A static list just shows data. Real apps let people remove items, and SwiftUI makes deletion feel native with a simple swipe. ✨

Meet onDelete

The onDelete modifier attaches to a ForEach inside a List. It enables the swipe-left gesture that reveals a red Delete button.

Your Data Lives in State

To delete a row you must own the data. Keep your array in @State so changing it instantly refreshes the list on screen.

struct Item: Identifiable {
    let id = UUID()
    var name: String
}

Declaring the Array

Hold your items in a mutable @State array. When you remove an element, SwiftUI animates the row away for free.

@State private var items = [
    Item(name: "Milk"),
    Item(name: "Eggs")
]

The IndexSet Argument

onDelete hands you an IndexSet of the rows the user swiped. It is a set because a person could remove several at once.

Removing by Offsets

Pass that IndexSet straight to the arrays remove(atOffsets:) method. One line removes exactly the swiped items safely.

func delete(at offsets: IndexSet) {
    items.remove(atOffsets: offsets)
}

Wiring It Together

Put your rows in a ForEach inside a List, then attach onDelete to call your delete function. That is the whole pattern.

List {
    ForEach(items) { item in
        Text(item.name)
    }
    .onDelete(perform: delete)
}

Inline Closures Work Too

You can skip the named function and write the deletion right inside onDelete as a closure when the logic is tiny.

.onDelete { offsets in
    items.remove(atOffsets: offsets)
}

Free Swipe Animation

Because items is @State, SwiftUI compares before and after and slides the deleted row out smoothly. You never write the animation yourself.

Attach to ForEach, Not List

A common slip is putting onDelete on the List. It belongs on the ForEach, since that is what maps your array to rows.

Custom Swipe Actions

For more than delete, reach for swipeActions, which lets you add tinted buttons like Archive or Flag on either edge.

.swipeActions {
    Button("Flag") { }
        .tint(.orange)
}

Quick Check

Where does the onDelete modifier belong?

Recap: Swipe-to-Delete

You learned to keep items in @State, attach onDelete to a ForEach, and remove swiped rows with remove(atOffsets:). 🎉

Frequently asked questions

Is the “Swipe-to-Delete Rows” lesson free?

Yes — the full text of “Swipe-to-Delete Rows” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.

What will I learn in “Swipe-to-Delete Rows”?

Remove items with onDelete. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SwiftUI Academy?

No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Swipe-to-Delete Rows” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SwiftUI Academy lesson?

Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Swipe-to-Delete Rows
  2. Reordering with onMove
  3. Adding New Items
  4. Pull-to-Refresh
← Back to SwiftUI Academy