Delete operation in aio files | Community
Skip to main content
October 9, 2024
Solved

Delete operation in aio files

  • October 9, 2024
  • 2 replies
  • 692 views
 
const { Core } = require('@adobe/aio-sdk')
const { errorResponse, stringParameters, checkMissingRequestInputs } = require('../utils')
const filesLib = require('@adobe/aio-lib-files')
async function main(params) {
 
const logger = Core.Logger('main', { level: params.LOG_LEVEL || 'info' })

try {
// 'info' is the default level if not set
logger.info('Calling the main action to delete Brief file')

// log parameters, only if params.LOG_LEVEL === 'debug'
logger.debug(stringParameters(params))

//Check for missing request input parameters and headers
const requiredParams = ['briefids']
const requiredHeaders = ['Authorization']
const errorMessage = checkMissingRequestInputs(params, requiredParams, requiredHeaders)
if (errorMessage) {
// return and log client errors
return errorResponse(400, errorMessage, logger)
}
const files = await filesLib.init()
const existingfile = await files.list('/brief/')
if (!existingfile.length) {
errorMessage('400', `can not perform delete operation`, logger)
}
else {
await files.delete(`brief/${params.briefids}.JSON`)
const body = []
for (let { name } of existingfile) {
if(name===(`brief/${params.briefids}.json`))
{
 
}
else
{
const buffer=await files.read(`${name}`)
body.push(JSON.parse(buffer.toString()))
}
}
return {
status: 200,
message: params.briefids,
body
}

}
 
}
catch (error) {
// log any server errors
logger.error(error)
// return with 500
return errorResponse(500, 'server error', logger)
}
}
exports.main = main
 
when I am performing a delete operation via---await files.delete(`brief/${params.briefids}.JSON`)
but I am getting all the listed files along with the deleted ones,
 
 
 
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by spaliwal

Hi Shubham,

As @estebanbustamante suggested, you will need to call the list method again.

In your code change 

const existingfile = await files.list('/brief/')

to 

let existingfile = await files.list('/brief/')

 

And after calling the delete method make following change

await files.delete(`brief/${params.briefids}.JSON`) existingfile = await files.list('/brief/')

 

2 replies

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
October 14, 2024

Hi

 

I think you should call the files.list method again after the delete operation to get the updated list of files.

Esteban Bustamante
spaliwalAdobe EmployeeAccepted solution
Adobe Employee
October 30, 2024

Hi Shubham,

As @estebanbustamante suggested, you will need to call the list method again.

In your code change 

const existingfile = await files.list('/brief/')

to 

let existingfile = await files.list('/brief/')

 

And after calling the delete method make following change

await files.delete(`brief/${params.briefids}.JSON`) existingfile = await files.list('/brief/')