# Cleanup after function cancellation
Source: https://www.inngest.com/docs/examples/cleanup-after-function-cancellation
Description: Create a function that executes after a function run has been cancelled via event, REST API, or bulk cancellation.

When function runs are cancelled, you may want to perform some sort of post-cancellation code. This example will use the [`inngest/function.cancelled`](/docs/reference/system-events/inngest-function-cancelled) system event.

Whether your function run is cancelled via [`cancelOn` event](/docs/features/inngest-functions/cancellation/cancel-on-events), [REST API](/docs/guides/cancel-running-functions) or [bulk cancellation](/docs/platform/manage/bulk-cancellation), this method will work the same.

## Quick snippet

Here is an Inngest function and a corresponding function that will be run whenever the original function is cancelled. This uses the function trigger's `if` parameter to filter the `inngest/function.cancelled` event to only be triggered for the original function.

```ts
new Inngest({ id: "newsletter-app" });

// This is our "import" function that will get cancelled
importAllContacts = inngest.createFunction(
  {
    id: "import-all-contacts",
    cancelOn: [{ event: "contacts/import.cancelled", if: "async.data.importId == event.data.importId" }]
  },
  { event: "contacts/import.requested" },
  async ({ event, step  }) => {
    // This is a long running function
  }
)

// This function will be run only when the matching function_id has a run that is cancelled
cleanupCancelledImport = inngest.createFunction(
  {
    name: "Cleanup cancelled import",
    id: "cleanup-cancelled-import"
  },
  {
    event: "inngest/function.cancelled",
    // The function ID is a hyphenated slug of the App ID w/ the functions" id
    if: "event.data.function_id == 'newsletter-app-import-all-contacts'"
  },
  async ({ event, step, logger }) => {
    // This code will execute after your function is cancelled

    // The event that triggered our original function run is passed nested in our event payload
    event.data.event;
    logger.info(`Import was cancelled: ${originalTriggeringEvent.data.importId}`)
  }
);
```

An example cancellation event payload:

```json
{
  "name": "inngest/function.cancelled",
  "data": {
    "error": {
      "error": "function cancelled",
      "message": "function cancelled",
      "name": "Error"
    },
    "event": {
      "data": {
        "importId": "bdce1b1b-6e3a-43e6-84c2-2deb559cdde6"
      },
      "id": "01JDJK451Y9KFGE5TTM2FHDEDN",
      "name": "contacts/import.requested",
      "ts": 1732558407003,
      "user": {}
    },
    "events": [
      {
        "data": {
          "importId": "bdce1b1b-6e3a-43e6-84c2-2deb559cdde6"
        },
        "id": "01JDJK451Y9KFGE5TTM2FHDEDN",
        "name": "contacts/import.requested",
        "ts": 1732558407003,
        "user": {}
      }
    ],
    "function_id": "newsletter-app-import-all-contacts",
    "run_id": "01JDJKGTGDVV4DTXHY6XYB7BKK"
  },
  "id": "01JDJKH1S5P2YER8PKXPZJ1YZJ",
  "ts": 1732570023717
}
```

## More context

Check the resources below to learn more about building email sequences with Inngest.

<ResourceGrid cols={2}>

<Resource resource={{
  href: "/docs/reference/system-events/inngest-function-cancelled",
  name: "Reference: inngest/function.cancelled system event",
  icon: RiBracesFill,
  description: "Learn more about the system event.",
  pattern: 1,
}}/>

<Resource resource={{
  href: "/docs/features/inngest-functions/cancellation",
  name: "Guide: Function cancellation",
  icon: RiBracesFill,
  description: "Learn about the different ways to cancel a function run.",
  pattern: 2,
}}/>

</ResourceGrid>