Skip to main content

deleteExperiment()

Deletes an experiment and its result records, then attempts to delete the observability traces produced by the experiment. Trace deletion cascades to spans and trace-linked signals. Unsupported observability storage leaves the traces in place and logs a warning.

Use dataset.deleteExperiment() when you have a Dataset instance. Use mastra.datasets.deleteExperiment() to delete by experiment ID without a dataset reference, including experiments orphaned by dataset deletion.

Delete from a dataset
Direct link to Delete from a dataset

import { Mastra } from '@mastra/core'

const mastra = new Mastra({/* storage config */})
const dataset = await mastra.datasets.get({ id: 'dataset-id' })

await dataset.deleteExperiment({ experimentId: 'experiment-id' })

The experiment must belong to the dataset. A missing experiment or an experiment associated with another dataset throws an error.

Parameters
Direct link to Parameters

experimentId:

string
ID of the experiment to delete.

Returns Promise<void>, which resolves when deletion completes.

Delete without a dataset reference
Direct link to Delete without a dataset reference

import { Mastra } from '@mastra/core'

const mastra = new Mastra({/* storage config */})

await mastra.datasets.deleteExperiment({
experimentId: 'experiment-id',
organizationId: 'organization-id',
projectId: 'project-id',
})

The manager method doesn't require the experiment to remain associated with a dataset. Use it to delete an orphaned experiment whose datasetId was cleared when its dataset was deleted.

When organizationId or projectId is provided, deletion is scoped to those values. A tenancy mismatch is a silent no-op.

Parameters
Direct link to Parameters

experimentId:

string
ID of the experiment to delete.

organizationId?:

string
Organization ID used to scope the deletion.

projectId?:

string
Project ID used to scope the deletion.

Returns Promise<void>, which resolves when deletion completes or when a tenancy-scoped request doesn't match the experiment.

Trace deletion support
Direct link to Trace deletion support

Before deleting the result records, Mastra collects their trace IDs for the cascade. Storage without observability or trace deletion support leaves those traces in place, logs a warning, and still deletes the experiment with its result records.