System Information
Environment: development
OS: linux-x64
Strapi Version: 5.6.0
Node/Yarn Version: npm/10.2.3 node/v20.10.0 linux x64 workspaces/false
Edition: Community
Database: sqlite
I cannot seem to delete a file that I uploaded through the Upload plugin’s REST API in Strapi v5 from a controller. The provider is set to local
and I added what I believe is the proper configuration to config/plugins.ts:
export default ({ env }) => ({
upload: {
config: {
provider: 'local',
providerOptions: {
maxFileSize: 10000000,
},
actionOptions: {
upload: {},
delete: true,
},
}
}
});
Here is my content type’s controller which is overriding the delete method:
import { factories } from '@strapi/strapi'
export default factories.createCoreController('api::certificate.certificate', ({ strapi }) => ({
async delete(ctx) {
try {
// Get certificate record (document) to determine the attachment (file) that it references
const certificate = await strapi.documents('api::certificate.certificate').findOne({
documentId: ctx.params.id, populate: 'attachment'
});
// Get file to verify it exists
const file = await strapi.documents('plugin::upload.file').findOne({
documentId: certificate.attachment.documentId,
});
if (!file) {
return ctx.notFound('File not found.');
}
// Delete the file from the database and file system
let result = await strapi.plugins.upload.services.upload.remove({ id: certificate.attachment.id });
console.log('result', result);
// Delete the certificate record now that the file is deleted
return await super.delete(ctx);
} catch (error) {
console.error('Error deleting file:', error);
ctx.internalServerError('An error occurred while deleting the file.');
}
},
}));
I’m not seeing any errors when this executes and the file entry in the database is deleted, yet the file remains in the file system and is still accessible with the URL.
If I use the Upload plugin’s REST API to delete the file instead of my controller code above, the file is properly deleted from the database and the file system. This proves that there isn’t a file system permission problem and the plugin seems to be configured correctly. I noticed it also updates the attachment field (media type) which is linked to the file, by setting it to null.
However this doesn’t accomplish deleting my certificate document since I’m calling the Upload plugin’s delete method.
I tried updating from Strapi v5.4.0 to v5.6.0 just in case but that did not solve my problem. I’ve searched documentation and this forum but haven’t found any answers.