Developing automations requires a Business+ or Enterprise Grid plan. Don't have one? Join the Developer Program and provision a sandbox with access to all Slack features for free.
There are a couple ways you can delete items from a datastore. You can:
Slack CLI commands
You can also delete items from a datastore with the datastore delete
and datastore bulk-delete
Slack CLI commands.
delete
and bulkDelete
There are two methods for deleting items in datastores:
apps.datastore.delete
method is used for single items.apps.datastore.bulkDelete
method is used for multiple items.They work quite similarly. In the following examples we'll be deleting items from a datastore via their primary key. Regardless of what you named your primary_key
, the query will always use the id
key.
delete
method to delete an item by its primary_key
// Somewhere in your function:
const uuid = "6db46604-7910-4684-b706-ac5929dd16ef";
const response = await client.apps.datastore.delete({
datastore: "drafts",
id: uuid,
});
if (!response.ok) {
const error = `Failed to delete a row in datastore: ${response.error}`;
return { error };
}
bulkDelete
method to delete an item by its primary_key
// Somewhere in your function:
const uuid = "6db46604-7910-4684-b706-ac5929dd16ef";
const uuid2 = "1111111-1111-1111-1111-111111111111";
const response = await client.apps.datastore.bulkDelete({
datastore: "drafts",
ids: [uuid,uuid2]
});
if (!response.ok) {
const error = `Failed to delete a row in datastore: ${response.error}`;
return { error };
}
If the call was successful, the payload's ok
property will be true
. If it is not successful, it will be false
and provide an error in the errors
property.
Datastore bulk API methods may partially fail.
The partial_failure
error message indicates that some items were successfully processed while others need to be retried. This is likely due to rate limits. Call the method again with only those failed items.
You'll find a failed_items
array within the API response. The array contains all the items that failed, in the same format they were passed in. Copy the failed_items
array and use it in your request.
You can set up your datastore to automatically delete records which are old and no longer relevant. This is done with the Time To Live (TTL) feature offered by AWS DynamoDB. Use it to efficiently discard data your app no longer needs.
For any item, define an expiration timestamp and the item will be automatically deleted once that expiration time has passed.
Notice that we didn't say immediately deleted. AWS only guarantees deletion of expired items 48 hours past the expiration date. If you query your table before 48 hours have passed, do not assume all expired items have been deleted. You can read more about this within the AWS documentation.
See below for an example on querying a database while filtering out any remaining expired items.
You can use a pre-existing attribute or add a new attribute.
The attribute's type must be set as Schema.slack.types.timestamp
in the datastore definition.
In this example, we're using an attribute called expire_ts
:
// /datastores/drafts.ts
import { DefineDatastore, Schema } from "deno-slack-sdk/mod.ts";
export default DefineDatastore({
name: "drafts",
primary_key: "id",
attributes: {
id: {
type: Schema.types.string,
},
expire_ts: {
type: Schema.slack.types.timestamp // This line!
}
},
...
});
time_to_live_attribute
to the selected attribute in the datastore definition// /datastores/drafts.ts
import { DefineDatastore, Schema } from "deno-slack-sdk/mod.ts";
export default DefineDatastore({
name: "drafts",
time_to_live_attribute: "expire_ts" // This line!
primary_key: "id",
attributes: {
id: {
type: Schema.types.string,
},
expire_ts: {
type: Schema.slack.types.timestamp
},
message: {
type: Schema.types.string,
}
},
...
});
expire_ts
to a value, either programmatically or manuallyIn this example we're adding an item containing the expire_ts
key using the apps.datastore.put
method:
const expiration =
const putResp = await client.apps.datastore.put<
typeof DraftDatastore.definition
>({
datastore: DraftDatastore.name,
item: {
id: draftId,
expire_ts: 23456432345,
message: "Congrats on the promotion Jesse!"
},
});
Use the slack deploy
command. See Deploy to Slack for more information.
As mentioned above, expired items may not be deleted immediately. You'll likely want to filter out those expired items.
Here is an example of a query that filters out any expired items that have not been automatically deleted yet:
const result = await client.apps.datastore.query({
datastore: "DraftDatastore",
expression: "attribute_not_exists(#expire_ts) OR #expire_ts > :timestamp",
expression_attributes: { "#expire_ts": "expire_ts" },
expression_values: { ":timestamp":1708448410 } //Timestamp should be the current time
});
To see an example of filtering out expired items via the command line, see the documentation on the datastore query
command.
time_to_live_attribute
in the datastore definitionWe only commented it out here because showing the absence of something is a bit anticlimactic.
export default DefineDatastore({
name: "drafts",
primary_key: "id",
attributes: {
id: {
type: Schema.types.string,
},
//expire_ts: {
// type: Schema.slack.types.timestamp
//}
},
...
});
Use the slack deploy
command. See Deploy to Slack for more information.
Due to AWS limitations, changing the TTL attribute is a bit clunky.
Step 1. Disable TTL
Step 2. Wait one hour. This wait is because AWS puts time limits on additional changes to the TTL feature.
Step 3. Enable TTL again with the new attribute
Don't forget to deploy both when disabling and enabling TTL!