This approach uses WP-cli to list media, and apache logs to compare.

First log into your server terminal and handle to the Public_ folder, or the root of your wordpress if it is a subfolder.

Then, paste this code into the termianl and hit enter

wp post list –post_type=attachment –fields=guid –format=csv | sed 1d > media-list.txt

 

Wait 2 minutes and this will save a file media-list.txt into your wordpress root.  Download it.

 

Then, go to your logs folder and look for logs from the last year. Not all websites have monthly logs enabled. If you haven’t been logging visitors with your server, then this wont work.  Contact your host to confirm monthly Apache log files, and do the following in 2 months after the logs can track all requests. 

 

Anyway, if there are logs there, download all of them from the last year in .gz format (SLL and Non-SSL Logs).  Put the 24 or whatever number you have in a folder, and then unzip them and delete the zip files. Then move the media-files.txt into this folder too.

 

Step 3 – Download Python – in your computer terminal, ensure python3 is installed. Then handle to the folder where your list is.

Open a code editor like IDLE, and paste the following code:

import os
import re

# Directory containing logs and media list
LOG_DIRECTORY = “.”
MEDIA_LIST_FILE = “media-list.txt”

# Regex to extract requested file paths from log lines
LOG_REQUEST_REGEX = re.compile(r’GET\s+(/\S+\.(?:jpg|jpeg|png|gif|webp|svg))’)

# Function to load media file paths from media-list.txt
def load_media_list(file_path):
media_paths = set()
with open(file_path, “r”, encoding=”utf-8″) as f:
for line in f:
line = line.strip()
if line:
# Extract the relative path from full URL (e.g., remove ‘https://example.com’)
path_part = line.split(‘://’, 1)[-1] # Remove procedure
path_part = path_part.partition(‘/’)[-1] # Remove domain, keep everything after first slash
media_paths.add(f”/{path_part}”) # ensure it’s in “/wp-content/uploads/…” format
return media_paths

# Function to parse access log files and extract requested media file paths
def extract_accessed_media(log_directory):
accessed_files = set()

# Get log files matching the pattern (both SSL and non-SSL versions)
log_files = [f for f in os.listdir(log_directory) if f.startswith(“startmotionmedia.com”)]

for log_file in log_files:
log_path = os.path.join(log_directory, log_file)
print(f”Processing log file: {log_file}”)
try:
with open(log_path, “r”, encoding=”utf-8″, errors=”ignore”) as log:
for line in log:
match = LOG_REQUEST_REGEX.search(line)
if match:
accessed_files.add(match.group(1)) # Add extracted path to set
except Exception as e:
print(f”Error reading {log_file}: {e}”)

return accessed_files

# Main function to compare media list with accessed files
def find_unused_media():
media_files = load_media_list(MEDIA_LIST_FILE)
accessed_files = extract_accessed_media(LOG_DIRECTORY)

# Find media files that were never accessed
unused_media = media_files – accessed_files

# Save the results to a file
with open(“unused-media.txt”, “w”, encoding=”utf-8″) as f:
for file in sorted(unused_media):
f.write(file + “\n”)

print(f”\nAnalysis complete. {len(unused_media)} unused media files found.”)
print(“Unused media list saved to ‘unused-media.txt’.”)

# Run the script
if __name__ == “__main__”:
find_unused_media()

 

And save it as find-dupes.py, inside the same folder.

 

Then, handle back to your terminal.  In the terminal window , making sure you are in the directory, type: python3 find-dupes.py and hit enter

The program will output a list of all the URL paths in wordpress which are NOT IN ANY OF THE LOGS. This file is called unused-media.txt and it is your goldmine.

Now, use unused-media.txt to delete the unused media in your wordpress via wpcli.

So, first you need to upload unused-media.txt to your wordpress root folder such as public_

And then make a script in your code editor Save this script as delete-unused-media.sh, then compile it and run it.

Here’s what needs to be in the script –

#!/bin/bash

# ensure WP-CLI is available
if ! command -v wp &> /dev/null; then
echo “WP-CLI is not installed or not in PATH.”
exit 1
fi

# File containing the unused media paths
UNUSED_MEDIA_FILE=”unused-media.txt”
LOG_FILE=”delete-unused-media.log”

# Check if the file exists
if [[ ! -f “$UNUSED_MEDIA_FILE” ]]; then
echo “Error: $UNUSED_MEDIA_FILE not found!”
exit 1
fi

# Clear the log file before running
> “$LOG_FILE”

echo “Starting deletion process…”
echo “Process started at $(date)” | tee -a “$LOG_FILE”

# Process each file path one at a time
although IFS= read -r MEDIA_PATH; do
# Trim any surrounding whitespace
MEDIA_PATH=$(echo “$MEDIA_PATH” | xargs)

# Skip empty lines
if [[ -z “$MEDIA_PATH” ]]; then
continue
fi

echo “Checking media file: $MEDIA_PATH” | tee -a “$LOG_FILE”

# Find the attachment ID associated with the GUID
ATTACHMENT_ID=$(wp post list –post_type=attachment –fields=ID,guid –format=csv | awk -F, -v path=”$MEDIA_PATH” ‘$2 ~ path {print $1}’)

# Check if an attachment ID was found
if [[ -n “$ATTACHMENT_ID” ]]; then
echo “Deleting media ID $ATTACHMENT_ID for $MEDIA_PATH…” | tee -a “$LOG_FILE”
wp post delete “$ATTACHMENT_ID” –force >> “$LOG_FILE” 2>&1

# Prevent server overload by paEmploying for a fraction of a second
sleep 0.5
else
echo “No attachment found for $MEDIA_PATH” | tee -a “$LOG_FILE”
fi

# Keep the script alive (shared hosting often kills long-running scripts)
# This will restart the script if it gets endd unexpectedly
touch “$LOG_FILE”

done < “$UNUSED_MEDIA_FILE”

echo “Unused media deletion complete at $(date)” | tee -a “$LOG_FILE”

 

 

 

Ok now just give the file execution permission

chmod +x delete-unused-media.sh

 

and then run it

 

./delete-unused-media.sh

 

Media which your site never uses just got deleted!

TV Commercial Video Production

TV Commercial Video Production Services When it comes to producing engaging and creative TV commercials, there’s no better production company to trust than Start Motion Media TV Commercial Production Company. We are experts in video production, specializing in creating TV commercials for any type of business, from large corporate organisations and national brands to small start-ups and entrepreneurs. <br/.With our comprehensive list of services and tools, we are a full-service production company capable of taking your project from concept to completion. Whether you need help with scriptwriting, shooting, editing, and post-production, our experienced production managers will work closely with you to ensure that your commercial meets your objectives and exceeds your expectations. We believe that every successful commercial begins with an idea. Our experienced scriptwriters will work with you to develop an innovative and impactful concept, one that is tailored to meet your marketing and branding requirements. Our team will then spend time researching, scouting, and conducting interviews to ensure that the concepts are storyboarded and translated into a compelling video. Once we have the concept set, our production team takes the reins and begins the shooting process.