Zach Tackett
Zach Tackett

Reputation: 614

Programmatically delete images from WordPress

I need to programmatically delete images from the WordPress Media Library. But, I'm at a loss as to how to do this.

To clarify: I need to be able to add code to my theme functions file or a custom plugin that will allow me to use the WordPress API / hooks to remove an image by the ID assigned to it by WordPress.

Upvotes: 3

Views: 10081

Answers (1)

Phill Healey
Phill Healey

Reputation: 3180

Here try this:

<?php wp_delete_attachment( $attachmentid, $force_delete ); ?> 

or if you want to just send the image to the trash:

<?php wp_delete_attachment( $attachmentid ); ?> 

WordPress Codex has more details: https://codex.wordpress.org/Function_Reference/wp_delete_attachment

Upvotes: 9

Related Questions