Unable to Delete Items in Joomla’s Trash

Note: This post applies to Joomla 2.5 only. If you’re running Joomla 3.x and you can’t delete articles from Joomla’s trash, then please let us know in the comment section of this post as the below solution will not work for you.

Yes – we know – our regular readers might say that we have discussed this before. But back then, we only thought that the inability to delete items from Joomla’s trash was a user error, and not an application error. Obviously, we were wrong – which explains why we are writing another article on the same exact subject!

So, earlier this week, a new client with a Joomla 2.5 website called us and told us that he’s not able to empty Joomla’s trash, even after ensuring that he filtered for “Trashed” articles before trying to empty the trash. We checked his website and we were able to easily verify the claim. Interesting…

So, we dug deeper and we found out that the issue was caused by the fact that many of the trashed articles did not have an entry in the assets table. So, just opening and saving one of those trashed items allowed us to remove it completely from the trash (note that when you open and save an item, Joomla re-creates its entry in the assets table if that entry is not already there).

But the problem is that our client literally had 20K trashed articles that need to be deleted and the absolute majority of these articles didn’t have an entry in Joomla’s assets table. Of course, resolving this problem is as easy as running the below query on the #__content table:

DELETE FROM #__content WHERE `state`='-2';

(Where #__ is the database prefix of your Joomla website as defined in its configuration.php file).

But doing the above is not what our client wanted, since he didn’t want to call us to run the above query each time he wanted to empty his trash. Additionally, executing the above query will affect the data integrity in the assets table, since some content item entries will remain there, but with no actual articles to be associated with.

So, our next job was to find where Joomla is checking for the article’s entry in the assets table before deleting it… It took us some time to find where that check was done: it was in the table.php file (which no longer exists in Joomla 3.x) under the libraries/joomla/database folder. The check was done in the function delete of the aforementioned file, specifically in these lines:

if ($this->_trackAssets)
{
	// Get and the asset name.
	$this->$k = $pk;
	$name = $this->_getAssetName();
	$asset = JTable::getInstance('Asset');

	if ($asset->loadByName($name))
	{
		if (!$asset->delete())
		{
			$this->setError($asset->getError());
			return false;
		}
	}
	else
	{
		$this->setError($asset->getError());
		return false;
	}
}

So, in order to address the problem, we modified the above code to:

if ($this->_trackAssets)
{
	// Get and the asset name.
	$this->$k = $pk;
	$name = $this->_getAssetName();
	$asset = JTable::getInstance('Asset');

	if ($asset->loadByName($name))
	{
		if (!$asset->delete())
		{
			$this->setError($asset->getError());
		}
	}
	else
	{
		$this->setError($asset->getError());
	}
}

Note that the return false; statement was removed in the fixed code.

But isn’t that a core change?

Yes – it is. But we assume that if one is still running Joomla 2.5, then he should expect core modifications to address some bugs. If you’re not comfortable with doing the core change, then you can just set up a cron that will regularly delete articles with a state of -2. Note that if you would like to preserve the integrity of the assets table, then you you should also delete the associated entries in the assets table.

But why would a content item not have an entry in the assets table?

Typically, there are 2 reasons for this to happen:

  1. You are deleting the content item entries from the assets table in order to optimize the content creation process on your Joomla website as described here.
  2. One of your extensions is creating content items (articles) in the Joomla CMS without adding an associated entry in the assets table.

Now, if you only have a few articles that you can’t delete from the trash, then just open these articles, save them, and then try to delete them again. If you have thousands of articles that you can’t delete and you are running Joomla 2.5 (or 1.6, or 1.7), then try the core modification above; it should work for you! If you’re not comfortable with core modifications, then just contact us and we shall implement an alternative solution for you in very little time and for a very small fee!

No comments yet.

Leave a comment