Skip to content

Introduce subcommands to list/delete orphaned entities #28

@tfrommen

Description

@tfrommen

Hi, again! 👋

As discussed in the #cli Slack channel, I hereby propose orphan subcommands for all entities that can be orphaned.

What Is This About?

WordPress offers dedicated APIs that are to be used for CRUD operations on the various core data structures. When deleting a comment via wp_delete_comment( $some_id_here, true ), WordPress takes care that all metadata for that comment gets automatically deleted as well. Great!

However, as we all know, people not always do what is right, and so it's not a rare situation that you have to face a database that is full of orphaned entries. Comment metadata for comments that no longer exist, comments for non-existing posts, or posts with parents that don't exist anymore.

With the new subcommands, you can easily spot orphans, and even delete them, if you want.

We would end up with the following:

  • wp comment orphan: comments for non-existing posts and/or with non-existing parents 🌟;
  • wp comment meta orphan: comment metadata for non-existing comments;
  • wp post orphan: posts with non-existing parents;
  • wp post meta orphan: post metadata for non-existing posts;
  • wp term meta orphan: term metadata for non-existing terms;
  • wp user meta orphan: user metadata for non-existing users.

🌟: Regarding comments, I don't know exactly what is the right definition of an orphan. A comment that has a non-existing parent? A comment that references a non-existing post? A comment with either?

Users don't have any reference, so they are no option.

Also, terms are not an option, for at least two reasons:

  • due to their bad special table structure, both finding and deleting orphaned terms is more complex than anything above;
  • it makes total sense to have terms that are not yet (or not anymore) in use (i.e., no post has that term assigned so far/any longer).

How to Use It

By default, wp %ENTITY% [meta] orphan will only list the IDs, one at a line, and don't do anything to these orphans:

$ wp comment orphan
4
8
15
16
23
42

By specifiying the count flag, the command informs about the number of found orphans, instead of listing their IDs:

$ wp post orphan --count
Entries found: 42

By specifying the delete flag, all orphans get removed from the database:

$ wp term meta orphan --delete
1
2
3
5
7
Entries deleted: 5

You can even combine the two flags:

$ wp user meta orphan --count --delete
Entries found: 123
Entries deleted: 123

How Does It Work?

Please have a look at #28 (db-command) to see my initial take on this.

The idea is now to split this up to include the individual parts in their respective entity command.


So, what do you say? 😃

Looking forward to feedback.

Cheers,
Thorsten

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions