WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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 '15 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.