3

I am in the process of learning WP-CLI and I typed in the command line mistakenly wp post generate instead of wp post generate --help

Now I have generated the default 100 posts, however, this is a good opportunity for me to try to delete these 100 posts using WP-CLI.

However, I could not figure it out. So is it possible to delete a range of posts by ID like for example from 1970 to 2070 or using the time they were generated as they are all generated at the same time all this with WP-CLI.

2

3 Answers 3

2

Nope, unfortunately not... I was looking for the same thing more or less (range by ID instead dates) and since wp media regenerate supports entering an ID range I thought it should be possible...

# Re-generate all thumbnails that have IDs between 1000 and 2000.
$ seq 1000 2000 | xargs wp media regenerate
Found 4 images to regenerate.
1/4 Regenerated thumbnails for "Vertical Featured Image" (ID 1027).
2/4 Regenerated thumbnails for "Horizontal Featured Image" (ID 1022).
3/4 Regenerated thumbnails for "Unicorn Wallpaper" (ID 1045).
4/4 Regenerated thumbnails for "I Am Worth Loving Wallpaper" (ID 1023).
Success: Regenerated 4 of 4 images.

You could export your posts by date range first via

wp export --dir=/tmp/ --user=admin --post_type=post --start_date=1970-01-01 --end_date=2070-12-31

then open the generated XML-file(s), select all <wp:post_id>XYZ</wp:post_id> in your editor (like SublimeText or VSCode), get rid of the markup and format the IDs as space-separated list and then use the gathered IDs to bulk delete like this

wp post delete 1001 1009 1028 1030 1044 1050

Very cumbersome, I know... Maybe create an issue over at the according Github-repo and request this feature?

2

For example, if you want to delete posts with IDs from 1970 to 2070, you would run below command

wp post delete $(seq 1970 2070) --force
1
  • Great share! Users should probably be careful with "ID" ranges though, because sometimes in WordPress the IDs are not in perfect order, and you might end up deleting a post you didn't realize was in that range. Commented Mar 22 at 7:38
0

It is possible with some complex WP-CLI commands.

Specifically, you can list a range of WordPress posts in a subshell using the wp post list command, and then delete that range of posts with wp post delete as below:

WARNING: BACKUP YOUR DATABASE BEFORE TRYING THIS!!!

wp post delete $(wp post list --post_type='post' --format=ids --posts_per_page=1)

In the above example, the most recent post will be deleted (not the oldest post). I strongly suggest you first test the subshell ouput beforehand like this:

wp post list --post_type='post' --format=ids --posts_per_page=1

This will tell you the ID number of the post that will be deleted. Check this ID number against the ID number of your most recent post (for example) if that's the post(s) you are wanting to delete. If all looks good, you can increase the number of posts you want to delete, e.g. --posts_per_page=10

Some posts types will cause a warning like:

Warning: Posts of type 'topic' do not support being sent to trash.

In that case you must append --force to your command, which will permanently delete them:

WARNING: PERMANENT DATA LOSS USING THE COMMAND BELOW!!!

wp post delete $(wp post list --post_type='topic' --format=ids --posts_per_page=10) --force
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.