XF 2.3 Properly remove orphaned threads

mjda

Well-known member
I have hundreds of threads, from a conversion, that have no posts with them. They don't even have the original post data associated with them. These are causing errors with the enhanced search, among other things.

So, I want to delete all those threads. What I'm thinking about doing is creating a script that will grab all threads with 0 replies, check to see if it has any associated posts and, if not, use $thread->delete();.

My question is, would this be the appropriate way to handle this to be sure that all stats are updated correctly? Will it still work correctly, even though there is missing post data?

Just for more info, here is an error that I get several times per day:

Code:
ErrorException: [E_WARNING] Attempt to read property "message" on null src/XF/Search/Data/Thread.php:111
Generated by: mjda Sep 4, 2024 at 8:02 AM

Code:
#0 src/XF/Search/Data/Thread.php(111): XF::handlePhpError(2, '[E_WARNING] Att...', '/home/mydirectory...', 111)
#1 src/XF/Search/Search.php(299): XF\Search\Data\Thread->getAutoCompleteResult(Object(XFMG\XF\Entity\Thread), Array)
#2 src/XF/ResultSet.php(129): XF\Search\Search->XF\Search\{closure}(Object(XFMG\XF\Entity\Thread), 'thread', 381)
#3 src/XF/Search/Search.php(285): XF\ResultSet->getResultsDataCallback(Object(Closure))
#4 src/XF/Pub/Controller/SearchController.php(123): XF\Search\Search->getAutoCompleteResults(Object(XF\ResultSet), Array)
#5 src/XF/Mvc/Dispatcher.php(362): XF\Pub\Controller\SearchController->actionAutoComplete(Object(XF\Mvc\ParameterBag))
#6 src/XF/Mvc/Dispatcher.php(264): XF\Mvc\Dispatcher->dispatchClass('XF:Search', 'AutoComplete', Object(XF\Mvc\RouteMatch), Object(XFES\XF\Pub\Controller\Search), NULL)
#7 src/XF/Mvc/Dispatcher.php(121): XF\Mvc\Dispatcher->dispatchFromMatch(Object(XF\Mvc\RouteMatch), Object(XFES\XF\Pub\Controller\Search), NULL)
#8 src/XF/Mvc/Dispatcher.php(63): XF\Mvc\Dispatcher->dispatchLoop(Object(XF\Mvc\RouteMatch))
#9 src/XF/App.php(2813): XF\Mvc\Dispatcher->run()
#10 src/XF.php(802): XF\App->run()
#11 index.php(23): XF::runApp('XF\\Pub\\App')
#12 {main}

Pretty much any time one of these orphaned thread titles is found to be one of the matches in the auto-complete search results, the entire thing fails. So, I noticed, as one example here, that a search through my database for posts with thread_id = 381 returns 0 results.

I used this query to search for more orphaned threads:

Code:
SELECT t.thread_id, t.title, p.post_id FROM xf_thread as t
left join xf_post as p on (p.thread_id = t.thread_id)
where p.post_id is null;

Turns out there are 1,588 of these in my database, with the most recent one being posted in 2014. So, at least it's not something that's still going on. Again, I'm pretty sure this all comes from the old database, and something happened during the conversion to XF. Maybe threads shouldn't be converted if they have no posts? Anyways, I just need to get it fixed now.
 
Last edited:
Solution
For others who encounter this issue and are looking for a solution...

I did write an add-on that would find all these orphaned threads (threads with no actual message content attached), and simply used $thread->delete() on them.

It appears as though everything worked fine, so far, and the errors are gone. The only issue I found while doing that was that my total messages count on my forum statistics dropped by the number of threads that were deleted, and I'm not sure it should have, since there were no actual posts deleted.

No big deal, but just thought I'd throw that out there.
For others who encounter this issue and are looking for a solution...

I did write an add-on that would find all these orphaned threads (threads with no actual message content attached), and simply used $thread->delete() on them.

It appears as though everything worked fine, so far, and the errors are gone. The only issue I found while doing that was that my total messages count on my forum statistics dropped by the number of threads that were deleted, and I'm not sure it should have, since there were no actual posts deleted.

No big deal, but just thought I'd throw that out there.
 
Solution
SELECT t.thread_id, t.title, p.post_id FROM xf_thread as t left join xf_post as p on (p.thread_id = t.thread_id) where p.post_id is null;

are you sure this query is correct? i ran it and it showed me a bunch of thread ids which are normal threads with posts when i checked.
 
SELECT t.thread_id, t.title, p.post_id FROM xf_thread as t
left join xf_post as p on (p.thread_id = t.thread_id)
where p.post_id is null;

are you sure this query is correct? i ran it and it showed me a bunch of thread ids which are normal threads with posts when i checked.

Yeah, I'm sure. That's not what I used when I deleted the threads, though. I used finders. That query did find 1,533 threads, though, and that's how many the add-on deleted when I ran it.
 
Back
Top Bottom