Post Media Cleanup removes orphaned media files from your server when you permanently delete a post.
When you permanently delete a post, this plugin automatically finds and removes:
Add extra attachments to the deletion list:
add_filter( 'postmediaweb_attachment_ids_to_delete', function( $ids, $post_id ) {
$extra = get_post_meta( $post_id, 'my_custom_pdf', true );
if ( $extra ) $ids[] = (int) $extra;
return $ids;
}, 10, 2 );
Prevent a specific attachment from being deleted:
add_filter( 'postmediaweb_should_delete_attachment', function( $should, $att_id, $post_id ) {
if ( $att_id === 999 ) return false;
return $should;
}, 10, 3 );