WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to run my function when a user deletes a media file from the media library. Is there a hook I can use?

Would appreciate it if someone could please point me to the right direction.


share|improve this question
I did some searching and it looks I should be hooking on before_delete_post. Will give it a go and reposrt back here – Sarah Neuber May 15 '12 at 16:09
up vote 0 down vote accepted

A more appropriate hook may be delete_attachment($post_id). You may also need to hook edit_attachment. Note that the post_id is that of the attachment type post, not the page/blog post to which the media is attached (if any).

In case you want to know when media is added, hook add_attachment($post_id). Note that add_attachment is called before intermediate size files are generated but after the media has been placed in the upload directory.

share|improve this answer
Indeed you are right Davidj! Thanks for that. I fixed my code and it works much better. – Sarah Neuber Sep 5 '12 at 6:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.