DeletePartition
DeletePartitionCommand is exposed as a quality of life improvement, but is NOT an official DynamoDB operation (eventhough we wish it was).
Use it with ⚠️ caution ⚠️ It can be long and costly on large partitions, and incomplete in case of inconsistent read.
Performs one or more Query operations on a Table, and then runs BatchWriteCommands to batch delete the returned items. Automatically iterates through query pages if needed:
import { DeletePartitionCommand } from 'dynamodb-toolbox/table/actions/deletePartition'
const deletePartitionCommand = PokeTable.build(
DeletePartitionCommand
)
await deletePartitionCommand.send()
Request
.query(...)
(required)
The partition to query, with optional index and range condition:
partition: The partition key to queryindex (optional): The name of a secondary index to queryrange (optional): If the table or index has a sort key, an additional Range or Equality Condition
// Delete 'ashKetchum' pokemons
await PokeTable.build(DeletePartitionCommand)
.entities(PokemonEntity)
.query({ partition: 'ashKetchum' })
.send()
See the QueryCommand documentation for more details.
.entities(...)
(required)
Provides a list of entities to filter the deleted items (via the internal entity attribute). Required to build underlying BatchDeleteRequests:
await PokeTable.build(DeletePartitionCommand)
// Deletes only `Pokemons` and `Trainers`
.entities(PokemonEntity, TrainerEntity)
.query(query)
.send()
.options(...)
Provides additional options:
await PokeTable.build(DeletePartitionCommand)
.options({
consistent: true,
...
})
.send()
You can use the DeletePartitionOptions type to explicitly type an object as a DeletePartitionCommand options object:
import type { DeletePartitionOptions } from 'dynamodb-toolbox/table/actions/deletePartition'
const queryOptions: DeletePartitionOptions<
typeof PokeTable,
[typeof PokemonEntity, typeof TrainerEntity]
// 👇 Optional query
{ partition: string }
> = {
consistent: true,
...
}
await PokeTable.build(DeletePartitionCommand)
.entities(PokemonEntity, TrainerEntity)
.query(query)
.options(queryOptions)
.send()
It is advised to provide entities and query first as they constrain the options type.
Available options (see the DynamoDB Query documentation for more details):
| Option | Type | Default | Description |
|---|---|---|---|
consistent | boolean | false | By default, read operations are eventually consistent (which improves performances and reduces costs).
|
capacity | CapacityOption | "NONE" | Determines the level of detail about provisioned or on-demand throughput consumption that is returned in the response.
|
tableName | string | - | Overrides the |
exclusiveStartKey | Key | - | The primary key of the first item that this operation evaluates. |
filters | Record<string, Condition> | - | For each entity name, a condition that must be satisfied in order for evaluated items of this entity to be deleted (improves performances but does not reduce costs).
|
entityAttrFilter | boolean | true | By default, specifying |
Examples
- Basic
- Consistent
- Index
- Filtered
- Multitenant
- Aborted
await PokeTable.build(DeletePartitionCommand)
.entities(PokemonEntity)
.query({ partition: 'ashKetchum' })
.send()
await PokeTable.build(DeletePartitionCommand)
.entities(PokemonEntity)
.query({ partition: 'ashKetchum' })
.options({ consistent: true })
.send()
await PokeTable.build(DeletePartitionCommand)
.entities(PokemonEntity)
.query({
index: 'byTrainerId',
partition: 'ashKetchum',
range: { gte: 50 }
})
.send()
await PokeTable.build(DeletePartitionCommand)
.entities(PokemonEntity, TrainerEntity)
.query({ partition: 'ashKetchum' })
.options({
filters: {
POKEMONS: { attr: 'pokeType', eq: 'fire' },
TRAINERS: { attr: 'age', gt: 18 }
}
})
.send()
await PokeTable.build(DeletePartitionCommand)
.entities(PokemonEntity)
.query({ partition: 'ashKetchum' })
.options({ tableName: `tenant-${tenantId}-pokemons` })
.send()
const abortController = new AbortController()
const abortSignal = abortController.signal
await PokeTable.build(DeletePartitionCommand)
.entities(PokemonEntity)
.query({ partition: 'ashKetchum' })
.send({ abortSignal })
// 👇 Aborts the command
abortController.abort()
Response
The response syntax is similar to the DynamoDB Query API, except that:
Itemsare not returned- The query
ConsumedCapacityis renamed asQueryConsumedCapacity - The batch write
ConsumedCapacityis renamed asBatchWriteConsumedCapacity
You can use the DeletePartitionResponse type to explicitly type an object as a DeletePartitionCommand response object:
import type { DeletePartitionResponse } from 'dynamodb-toolbox/table/actions/deletePartition'
const deletePartitionResponse: DeletePartitionResponse<
typeof PokeTable,
// 👇 Query
{ partition: 'ashKetchum' },
// 👇 Optional entities
[typeof PokemonEntity],
> = { Count: ... }