How can I prevent deletion of media files if they are in use?

System Information
  • Strapi Version: 4.9.0
  • Operating System: Mac 13.4.1
  • Database: Postgres
  • Node Version: v16.19.0
  • NPM Version: 8.19.3
  • Yarn Version: 1.22.19

Hey all. I’m having issue where our content creators can accidentally delete images that are being used in components.
I would like to be able to add a custom validation when some one tries to delete an image we do not allow it. More complex, it would be great to throw an error only if the image is in use somewhere.

I am using the strapi upload tool.

Usually for this, I would use the lifecycle methods, but for those to work, I need to be able to put a lifecycle.ts file in the model dir, but I don’t own the model dir for this resource so I cannot do that.

I see there are docs where I can subscribe to models on boot (Models | Strapi Documentation) by adding the name of the model to the models: [] list, but I cannot for figure out what magic string I need to type in here to add the hooks.

I am open to different options for solving this, so any help is welcome.

Thank you

It looks like the model name for uploads is plugin::upload.file if anyone else is looking for it.
I was able to find this by adding the following to src/index.ts

async bootstrap({ strapi }) {
    // docs on adding lifecycle hooks here for any model
      // models: [],

      async beforeCreate(event) {
        throw new Error('EERRROOOORRR')

If the models key is left off, all models are subscribed. I then went into the Strapi UI and tried uploading and image. This lifecycle hook fired and logged the event, from there I was able to find the name of the model that I needed.

So I seem to be experiencing a bit of a bug (or at least confusing behavior) with uploads.
I am able to catch the beforeDelete hook for plugin::upload.file, and throw an error. This is preventing the data from being removed from the database, but the underlying image is still being deleted from local storage.
Strapi appears to be deleting the file itself first before firing the lifecycle hook which still leaves me needing to find out how to be able to prevent deletions of uploaded files.