Delete

Overview

Delete tools permanently remove an entity. They follow the naming pattern of a Delete prefix and a singular entity name, such as DeletePrayerRequest. Because deleting is destructive, these tools should always carry an [AgentGuardrail] so the language model treats them carefully. The pattern is simple: load the required entity, delete it, and save.

[Description( "Deletes a prayer request from the system." )]
[AgentToolGuid( "423AFDB5-1095-4D55-8631-4F284FC0AFED" )]
[AgentGuardrail( "This action will permanently delete the specified prayer request. Ensure that this action is intentional and that you have the correct prayer request identifier before proceeding." )]
public AgentToolResult DeletePrayerRequest( string prayerRequestIdKey )
{
    using var rockContext = RockApp.Current.CreateRockContext();
    var helper = new AgentToolHelper( rockContext, AgentRequestContext, _logger );
    var prayerRequestService = new PrayerRequestService( rockContext );

    var existingPrayerRequest = helper.GetRequiredEntity<PrayerRequest>( prayerRequestIdKey, checkSecurity: true );

    if ( helper.HasErrors )
    {
        return helper.ErrorResult;
    }

    prayerRequestService.Delete( existingPrayerRequest );

    try
    {
        rockContext.SaveChanges();
    }
    catch ( Exception ex )
    {
        _logger.LogError( ex, "An error occurred while deleting a prayer request." );
        return Error( "An error occurred while deleting the prayer request." );
    }

    return Success( "The prayer request has been deleted." );
}

We load the entity with GetRequiredEntity, passing checkSecurity: true so the person's authorization is verified before anything is removed. After checking for errors, we delete through the entity's service and save inside a try/catch, returning a clear error if the save fails. A short success message is enough for the model to confirm the outcome to the user.

Warning

Delete tools are permanent and cannot be undone. Always attach an [AgentGuardrail], require the entity's IdKey rather than inferring it, and think hard before adding a delete tool to a Public agent. When in doubt, have the agent confirm intent with the user before calling a delete tool.