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!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.