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()
user.delete(
soft: Bool = false,
completion: ((Swift.Result<UserImpl, Error>) -> Void)? = nil
) -
deleteUser()
chat.deleteUser(
id: String,
soft: Bool = false,
completion: ((Swift.Result<UserImpl, Error>) -> Void)? = nil
)
Input
Parameter | Type | Required in delete() | Required in deleteUser() | Default | Description |
---|---|---|---|---|---|
id | String | No | Yes | n/a | Unique user identifier (up to 92 UTF-8 characters). |
soft | Bool | No | No | false | 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
Type | Description |
---|---|
((Swift.Result<UserImpl, Error>) | For hard delete, the method returns the last version of the User object before it was permanently deleted. For soft delete, an updated user instance with the status field set to deleted . |
Basic usage
Permanently delete user support_agent_15
.
-
delete()
show all 50 lines// Create PubNub configuration
let pubNubConfiguration = PubNubConfiguration(
publishKey: "your-publish-key",
subscribeKey: "your-subscribe-key",
userId: "your-user-id"
// Add other required parameters
)
// Create Chat configuration
let chatConfiguration = ChatConfiguration(
// Fill in the necessary parameters for ChatConfiguration
)
// Create ChatImpl instance
let yourChat = ChatImpl( -
deleteUser()
// Reference the "chat" object and directly invoke the "deleteUser()" method
chat.deleteUser(id: "support_agent_15") { result in
switch result {
case let .success(user):
debugPrint("User with ID: \(user.id) was successfully deleted")
case let .failure(error):
debugPrint("Failed to delete user: \(error)")
}
}
Other examples
Archive (soft delete) the user with an ID of support_agent_15
, keeping their data in the App Context storage.
-
delete()
show all 24 lines// reference the "chat" object and invoke the "getUser()" method
chat.getUser(userId: "support_agent_15") { result in
switch result {
case let .success(user):
if let user = user {
debugPrint("Fetched user metadata with ID: \(user.id)")
// Delete the fetched user instance
user.delete(soft: true) { result in
switch result {
case let .success(deletedUser):
debugPrint("User with ID: \(deletedUser.id) was successfully deleted")
case let .failure(error):
debugPrint("Failed to delete user: \(error)")
} -
deleteUser()
// Reference the "chat" object and directly invoke the "deleteUser()" method
chat.deleteUser(id: "support_agent_15", soft: true) { result in
switch result {
case let .success(user):
debugPrint("User with ID: \(user.id) was successfully deleted")
case let .failure(error):
debugPrint("Failed to delete user: \(error)")
}
}