Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a function wp_handle_upload() for uploads files.

$file = $_FILES['attachment_icon-' . $i];
$upload = wp_handle_upload($file, array('test_form' => false));

what is a function for delete a uploaded file?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Use wp_delete_attachment( $post_id ) if you have used wp_insert_attachment() before.
$post_id is the attachment ID.

If you haven’t used wp_insert_attachment() a simple …

unlink( $upload['file'] );

… will do it.

share|improve this answer
wp_delete_attachment does not remove the file if it was not added via wp_handle_upload. It was added using wp_insert_attachment, but it was uploaded beforehand via file_put_contents (file received in base64). Should I just use unlink then? But then relations in DB will stay for old files? – trainoasis Nov 16 at 8:38

there is no designated WP function, simply because you already have a PHP one, with the following syntax:


for full doc see the PHP manual:

share|improve this answer
This would leave dead entries in the database if it is already registered. – toscho Jan 8 '12 at 21:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.