I have table with GSI and trying to delete item using GSI as below.
But I get following error.
"The provided key element does not match the schema".
I tried to query using following keyword and worked.
So I already confirmed the provided key element is right.
I tried to search whether delete operation by GSI is possible or not but could not find good documentation.
Could anyone tell me what is the best approach to delete item using GSI?
async function deleteProject(projectDB:string,projectId:string):Promise<any>{
const params={
TableName:projectDB,
IndexName:'projectId-index',
Key:{
'projectId':projectId,
},
ExpressionAttributeNames: { '#a': 'projectId' },
ExpressionAttributeValues: {":val": projectId},
ConditionExpression:"#a = :val",
}
const result=await db.delete(params).promise();
console.log('result',result);
return result;
}
CodePudding user response:
You can't delete items through secondary indexes. Secondary indexes act like a read-only view of the base table, so if you want to delete data, you'll have to delete it from the base table.
Ideally you project the key attributes from the base table into the Global Secondary Index. If you want to delete an item, you can just delete it from the base table using the values you can see in the GSI.
Note: This will delete the item from the base table and thus from all secondary indexes as well.
If you just want to remove it from the GSI, you need to remove the attributes that are used as the keys for the GSI from the item in the base table, then it won't be replicated to the GSI.