Hi @development14,
To delete specific images and their metadata from certain folders in the wp-uploads directory using WP-CLI, you can use a combination of WP-CLI commands with Unix commands. WP-CLI doesn’t have a built-in command specifically for deleting media files by directory or folder, so we’ll need to use a workaround.
Here’s an example command you can use:
cd /path/to/your/wordpress/installation/wp-content/uploads
# Delete images and metadata in wp-uploads/2021/05
wp media delete $(wp media list --field=ID --path=2021/05)
# Delete images and metadata in wp-uploads/2021/06
wp media delete $(wp media list --field=ID --path=2021/06)
# Delete images and metadata in wp-uploads/2021/07
wp media delete $(wp media list --field=ID --path=2021/07)
Make sure to replace /path/to/your/wordpress/installation/ with the actual path to your WordPress installation directory.
Thanks!
Thank you Harsh, that’s great, will run those shortly and keep you posted
Hi Harsh, I believe I have the correct path to folder, but it says that the file isn’t found?
I have navigated to the folders /uploads/, /2021/ and /05/ but none of them have worked?
Here is the message I have received each time?
wp media delete $(wp media list –field=ID –path=2021/05)
-bash: /home/inekyjxs/wp-cli/bin/wp: No such file or directory
-bash: /home/inekyjxs/wp-cli/bin/wp: No such file or directory
/home/serverusername/public_html/wp-content/uploads is the path I am in
@harshgajipara In case you get a chance to reply, thanks again for your initial post