Support

Account

Home Forums Front-end Issues Delete Uploaded Files From Media Library Reply To: Delete Uploaded Files From Media Library

  • Hi @tatimenabi

    If $old_value returns a file URL, you could look to use attachment_url_to_postid()

    Once you have the attachment ID, you can then use wp_delete_attachment()

    Something like this:

    <?php
    function wpse_83587_delete_file( $value, $post_id, $field  ) {
        $old_value = get_field( $field['upload_my_file'], $post_id, false /* Don't format the value, we want the raw ID */ );
    
        if ( $old_value ){
    		
    		$attachment_id = attachment_url_to_postid( $old_value['url'] );
    		wp_delete_attachment( $attachment_id, 'true' );
    	}
    
     
    }
    
    add_filter( 'acf/update_value/type=file', 'wpse_83587_delete_file', 10, 3 );

    Code untested but may help!