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

Is there a way to delete a Post From Front-End and it's attachments permanently? This is a snippet that moves the post to the trash can, but it doesn't remove attached images (they remain on the server) and it doesn't remove the post permanently? Could someone please help?

$url = get_bloginfo('url');
if (current_user_can('edit_post', $post->ID)){
echo '<a class="delete-post" href="';
echo wp_nonce_url("$url/wp-admin/post.php?action=delete&post=$id", 'delete-post_' . $post- 
echo '">Delete post</a>';
share|improve this question
up vote 2 down vote accepted


<?php if (current_user_can('edit_post', $post->ID)) echo "<a href='" . wp_nonce_url("/wp-admin/post.php?action=delete&amp;post=$id", 'delete-post_' . $post->ID) . "'>Delete post</a>" ?>

You can decide when to empty the WordPress trash by adding this code to the wp-config.php file in your WordPress root directory.

define('EMPTY_TRASH_DAYS', 1 );

The 1 in the code signifies you want to empty the trash everyday. If you set to 0, the trash functionality will be disabled.

Finally, WordPress doesn't delete images when they are no longer attached to a page. See this ticket for an explanation: http://core.trac.wordpress.org/ticket/12108 Gist being that

media files may be used by other posts as well, which is why they must be deleted in the media library. If we changed it so that deleting a file from a post deleted it altogether from the system, it would break the existing behavior and cause a lot of unintentional deletions.

If you want to go against that rational, you can add this to your functions.php:

function delete_post_children($post_id) {
    global $wpdb;

    $ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_parent = $post_id AND post_type = 'attachment'");

    foreach ( $ids as $id )
add_action('delete_post', 'delete_post_children');

Also see Upload Janitor if you want to go the plug-in route for deleting unattached images.

share|improve this answer

There is no need to call the database manually, this works too:

add_action( 'delete_post', 'delete_post_children' );

function delete_post_children( $post_id )
    foreach( get_attached_media( '', $post_id ) as $attachment )

        wp_delete_attachment( $attachment->ID );
share|improve this answer

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.