How to delete an object from SwiftData in Swift

A step by step guide on deleting an object from a SwiftData persistent storage container using SwiftUI, UIKit or AppKit.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 09/12/2023 at 15:54
Last Updated on 09/12/2023 at 17:41
<p>Three flowers that represent Swift, Swift Data and Xcode.  The text "Delete an Object from a SwiftData Persistent Container" sits beneath them.</p>

A step by step guide on deleting an object from a SwiftData persistent storage container using SwiftUI, UIKit or AppKit.

SubscribeCheck out our SwiftData Quick Start Guide
<p>A screenshot of an iPhone showing the result of the sample app available on our open source swift GitHub repository.</p>

The following tutorial has been made available, along with a simple app for demonstrating SwiftData, through the tutorial/swift-data-basics branch on our Open Source Swift Starter Project.

git clone git@github.com:delasign/swift-starter-project.git
View RepositoryHow to create a SwiftLint enabled Swift XCode Project

Step One: Setup the Project

<p>A screenshot of Xcode highlighting the code that is available in a snippet below which allows you to check if a SwiftData object of a specific Id exists within the SwiftData persistent storage container.</p>

Follow the tutorials linked below to add SwiftData to your project, along with the ability to search for all objects, a specific object as well as if an object exists.

How to add an object to SwiftData in Swift

Step Two: Add the Delete Functionality

<p>A screenshot of Xcode highlighting the sample functionality that demonstrates how to delete SwiftData objects from a persistent container. The code snippet is available below.</p>

Add functionality similar that below to the project to check if an object exists within your SwiftData persistent storage container.

We recommend adding this to a file called DataCoordinator+SwiftData.swift, where DataCoordinator is the name of the MainActor singleton that manages the SwiftData.

Please note that the code snippet found above uses a custom error that we created to track SwiftData errors. To learn how to create custom errors, consult the tutorial below.

How to create custom errors in Swift

Step Three: Test

<p>A screenshot of an iPhone showing the result of the sample app available on our open source swift GitHub repository.</p>

Add functionality to delete SwiftData objects by calling the function created in Step Two.

Then, run the app and test that it works.

Looking to learn more about things you can do with SwiftData, Swift or XCode ?

Consult our quick start guide or search our blog to find educational content on learning how to use SwiftData, Swift or XCode.

Consult our SwiftData Quick Start GuideSearch our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe