If you’re blogging regularly, one of the biggest challenges is managing uploaded files (attachments). Whether you’re sharing images, PDF files, coding files, or downloadable content, your Media Library can quickly become cluttered.

For websites focused on file sharing, this problem grows even bigger. With frequent theme changes, WordPress generates multiple image versions, leading to duplicate files and wasted server space.
Now, here’s the real issue:
When you delete a post in WordPress, it only deletes the post content — not the attached media files.
Most users follow this path:
- Convert the post to Draft
- Move it to the Trash
- Then delete it Permanently
However, this only removes the post. All attached files like images or PDFs still remain in your Media Library.
Why This is a Problem
Unused media files:
- Increase server load
- Consume more disk space
- Slow down your website
- Waste your hosting resources
There are plugins available that can clean unused files, but they come with their own limitations and risks. In this post, we’ll show you how to delete a WordPress post along with its attachments — without using any plugins.
Manual Method (via Dashboard)
This method is simple and safe, but you must manually delete the media files.
Steps:
- Go to Posts > All Posts
- Hover over the desired post and click Trash
- Go to Media > Library
- Find and delete each file manually that was used in the post
Limitation: WordPress does not automatically delete media files when a post is deleted. These files stay in your Media Library even if the post is gone.
Custom Code Method (Delete Post + Attachments Automatically)
Now let’s solve the problem with a custom PHP snippet.
Step 1: Locate Your functions.php
File
The functions.php
file is found inside your active WordPress theme:
/wp-content/themes/your-active-theme/functions.php
You can access it via:
- WordPress Dashboard: Appearance > Theme File Editor
- cPanel > File Manager
- FTP client (like FileZilla)
Step 2: Add the Code
Paste the following code at the end of your functions.php
file:
function delete_post_and_attachments($post_id) {
// Check if it's a post and not a revision
if (get_post_type($post_id) != 'post' || wp_is_post_revision($post_id)) {
return;
}
// Get all attachments for the post
$attachments = get_children([
'post_parent' => $post_id,
'post_type' => 'attachment',
]);
// Delete each attachment
foreach ($attachments as $attachment) {
wp_delete_attachment($attachment->ID, true);
}
}
// Hook into post delete
add_action('before_delete_post', 'delete_post_and_attachments');
What This Code Does:
- When you delete a post, it checks for any attached media files
- It deletes all media files associated with that post automatically
- No manual Media Library cleanup needed
Important Note:
- Use this method only if you’re comfortable with editing theme files
- Always backup your site before making changes to
functions.php
- For safer use, consider running this code via a plugin like Code Snippets
Managing WordPress media files manually can become frustrating — especially when deleted posts leave behind orphaned images and PDFs.
By using this code snippet, you can automate the cleanup process, keep your media library tidy, and reduce server load — all without installing extra plugins.