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 a paginated Query Operation on a Table
and run subsequent BatchWriteCommands
to batch delete returned items:
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)
.query({ partition: 'ashKetchum' })
.entities(PokemonEntity)
.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)
.query(query)
// Deletes only `Pokemons` and `Trainers`
.entities(PokemonEntity, TrainerEntity)
.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)
.query(query)
.entities(PokemonEntity, TrainerEntity)
.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 |
- Strongly consistent
- On index
- Filtered
- Multitenant
- Aborted
await PokeTable.build(DeletePartitionCommand)
.query({ partition: 'ashKetchum' })
.entities(PokemonEntity)
.options({ consistent: true })
.send()
await PokeTable.build(DeletePartitionCommand)
.query({
index: 'byTrainerId',
partition: 'ashKetchum',
range: { gte: 50 }
})
.entities(PokemonEntity)
.send()
await PokeTable.build(DeletePartitionCommand)
.query({ partition: 'ashKetchum' })
.entities(PokemonEntity, TrainerEntity)
.options({
filters: {
POKEMONS: { attr: 'pokeType', eq: 'fire' },
TRAINERS: { attr: 'age', gt: 18 }
}
})
.send()
await PokeTable.build(DeletePartitionCommand)
.query({ partition: 'ashKetchum' })
.entities(PokemonEntity)
.options({ tableName: `tenant-${tenantId}-pokemons` })
.send()
const abortController = new AbortController()
const abortSignal = abortController.signal
await PokeTable.build(DeletePartitionCommand)
.query({ partition: 'ashKetchum' })
.send({ abortSignal })
// 👇 Aborts the command
abortController.abort()
Response
The response syntax is similar to the DynamoDB Query API, except that:
Items
are not returned- The query
ConsumedCapacity
is renamed asQueryConsumedCapacity
- The batch write
ConsumedCapacity
is 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: ... }