Skip to main content
This section implements the Delete method. Delete is a synchronous operation that removes a resource from the infrastructure.

What Delete does

The agent calls Delete to remove a resource when:
  • A user runs formae destroy
  • A resource is removed from a forma file and formae apply is run
  • A stack is being torn down

DeleteRequest

The agent sends a DeleteRequest when removing a resource:
Delete returns a ProgressResult. On success, set OperationStatusSuccess. On failure, set the appropriate ErrorCode. Notably, if the resource doesn’t exist, return NotFound (the agent treats this as success since the desired state is achieved).

Test

Add a test to sftp_test.go:

Test pattern

Implementation

Verify

Run the test to confirm Delete removes the file successfully:
The test creates a file, deletes it through the plugin, then confirms it’s gone by attempting to read it:

Handling NotFound

Now we need to handle another edge case: what happens when deleting a resource that doesn’t exist?

Why NotFound on Delete is special

The agent treats NotFound on Delete as success. Why? Because the desired outcome (resource gone) is already achieved. Whether formae deleted it or someone deleted it out-of-band, the end state is the same. This makes Delete idempotent: calling Delete multiple times has the same effect as calling it once.

NotFound test

NotFound implementation

Add a check at the beginning of Delete to detect if the file exists:

Verify

Summary

The idempotent behavior ensures that:
  • Retrying a delete is safe
  • Out-of-band deletions don’t cause errors
  • The agent can reconcile state correctly

Next: 09 - List - Implement List for resource discovery