• Resolved lfbender

    (@lfbender)


    I am trying to regenerate a PDF file whenever a post is being saved/updated. The PDF file name changes according to post_meta data, therefore I want to delete the existing PDF attachment AND the file from the server when a post is saved/updated, before the pdf gets regenerated and attached.

    wp_delete_attachment() deletes the attachment alright, but the file stays on the server even when forced to delete.

    I also tried wp_delete_file_from_directory( $file, $path); It returns true for having deleted the file, but the file stays on the server. Same for wp_delete_file();

    The only thing that seemed to be working was unlink(), but that creates another problem, because in case the file name doesn’t change, unlink() seems to put a stop to creating the file with the same name.

    
           wp_update_post( $my_post );
    
                if(get_post_status( $post_id ) == "publish"):
                        
                        $existing_PDFs = get_attached_media('application/pdf', $post_id);
    
                        foreach($existing_PDFs as $pdf):
    
                           $file = get_attached_file($pdf->ID, true);
                           $path = pathinfo($file, PATHINFO_DIRNAME);
    
                           wp_delete_file_from_directory( $file, $path);
    
                           wp_delete_file( $file );
                           
                           wp_delete_attachment($pdf->ID, true);
    
                        endforeach;
    
                        include('generate-single-machine-pdf.php');
                endif;
    

    What’s the secret to get WordPress to delete a file from the server along with the attachment?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    wp_delete_file() is essentially just a wrapper for unlink(), so there shouldn’t be any difference in behavior. Aggressive disk caching could prevent immediate reuse of an unlinked filename. It does not explain complete failure to delete though. Have you verified the file permissions are correct?

    Instead of first deleting all attached PDFs for a post, what if you opened all new PDF files for overwriting, while keeping track of the files newly written. Afterwards get all attached PDFs for the post, old and new, and delete every attachment and unlink every file that is not in the new list? Thus you only unlink filenames that have not been reused.

    Thread Starter lfbender

    (@lfbender)

    @bcworkz Thank you. Although I didn’t use the exact way you have described, you helped me find a way that works. I used filenames and filepaths to identify if the PDF will have a different title or not and unlinked based on that. If it is just an update, it just overwrites the file, no unlinking needed, if it is a file with a new name, it deletes the old attachment.

    Moderator bcworkz

    (@bcworkz)

    Sure, that’s good too. Same idea, different approach. You’re welcome.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Buil in functions not deleting file of attachments’ is closed to new replies.