Skip to main content

Deleting state

This section provides information on how to delete state.

info

In the context of a task, command or step, please read the following sections:

tip

Please note that the state stores support automatic cleanup of state entries:

  • Using the time-to-live (TTL) configuration on the state entries.
  • Using the task configuration, cleanupStateAfterSeconds: cleanup after the task completes.

API reference:

Required scopes:

  • Data/Buildby.Taskurai/state/delete

When using transactions, these additional scopes are required:

  • Data/Buildby.Taskurai/state/read

Prerequisites

Deleting a state entry by id

To delete a state entry by using the state id.

// Get state client
var stateClient = taskurai.GetStateClient();

await stateClient.DeleteStateAsync("DefaultStateStore", "ml::model::timeseries::forecasting::v1");

Deleting a state entry by state reference

When a state entry is returned, it contains an easy to use state reference, this reference can be used in most calls, like the delete method.

// Get state client
var stateClient = taskurai.GetStateClient();

var stateReference = new StateReference("DefaultStateStore", "ml::model::timeseries::forecasting::v1");

stateClient.DeleteState(stateReference);

Deleting bulk state in a transaction

It is possible to use transactions to save/update or delete state entries.

warning
  • When using transactions, all state entries must have the same partition key.
  • Maximum number of operations in a transactional batch: 100 (Cosmos DB)
var sessionId = "85909b63-91a1-4186-ba4a-5013d49e3a00";
var partitionKey = $"webshop::session::{sessionId}";

// Start a transaction, add customer bag, cart and remove coupon
var stateTransactionInputList = new List<StateTransactionInput>()
{
new StateTransactionInput()
{
Delete = new StateDeleteOperation($"webshop::session::{sessionId}::customer")
},
new StateTransactionInput()
{
Delete = new StateDeleteOperation($"webshop::session::{sessionId}::cart")
}
};

var stateBulkTransactionInput = new StateBulkTransactionInput(stateTransactionInputList)
{
PartitionKey = partitionKey,
MaxParallelism = 10 // Maximum parallelism to handle blobs server side
};

var stateResponseList = stateClient.BulkTransaction("DefaultStateStore", stateBulkTransactionInput);

Next, run the console application to validate:

dotnet run