Delete users
Two methods let you remove an existing user: delete()
and deleteUser()
. You can remove the user either with or without deleting their historical data from the App Context storage.
Both of these methods give the same output. The only difference is that you call a given method either on the Chat
(deleteUser()
) or the User
(delete()
) object. Depending on the object, these methods take a different set of input parameters - you either have to specify the user ID you want to delete or not because it's already known.
Requires App Context
To store data about users, you must enable App Context for your app's keyset in the Admin Portal.
Method signature
These methods take the following parameters:
-
delete()
(on theUser
object)user.delete(
soft: Bool = false
) async throws -> UserImpl? -
deleteUser()
(on theChat
object)chat.deleteUser(
id: String,
soft: Bool = false
) async throws -> UserImpl?
Input
Parameter | Required in delete() | Required in deleteUser() | Description |
---|---|---|---|
id Type: String Default: n/a | No | Yes | Unique user identifier (up to 92 UTF-8 characters). |
soft Type: Bool Default: false | No | No | Define if you want to permanently remove user metadata. The user metadata gets permanently deleted from the App Context storage by default. If you set this parameter to true , the User object gets the deleted status, and you can still restore/get their metadata. |
Output
Parameter | Description |
---|---|
UserImpl | For hard delete, the method returns nil . For soft delete, an updated user instance with the status field set to deleted . |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Permanently delete user support_agent_15
.
-
delete()
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let user = try await chat.getUser(userId: "support_agent_15") {
try await user.delete(soft: false)
} else {
debugPrint("User not found")
}
} -
deleteUser()
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
try await chat.deleteUser(id: "support_agent_15")
}
Other examples
Archive (soft delete) the user with an ID of support_agent_15
, keeping their data in the App Context storage.
-
delete()
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let user = try await chat.getUser(userId: "support_agent_15") {
let updatedUser = try await user.delete(soft: true)
debugPrint("User marked as deleted")
debugPrint("Updated user object: \(String(describing: updatedUser))")
} else {
debugPrint("User not found")
}
} -
deleteUser()
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
try await chat.deleteUser(id: "support_agent_15", soft: true)
}