Deleting Extended Attributes in MuraCMS

November 29, 2016

I’ve been working on some Mura plugins (more on that later), and a common thing that plugins do is create new Extended Attributes for a Page in a Mura site.  Mura being “non-destructive by design”, when you uninstall a plugin, the Extended Attributes don’t get deleted when the plugin goes away.  So if you’re (as I was) developing a plugin, and changing the names of Attributes, reinstalling, over and over, you can send up with “dangling” Extended Attributes on a Page that you’ll never use, are named wrong, could cause other problems, might be duplicates of other Attributes, etc.  

There’s no way in Mura to delete a Set of Extended Attributes off a page, but after some digging, I found that you can do this with just a little big of SQL code. like so:

SET @extendSetName = ‘[Name of the Set of Extended Attributes I want to delete]’;
SET @siteID = ‘[siteID]’;
SET @extendSetID = ( SELECT extendSetID FROM tclassextendsets WHERE name = @extendSetName AND siteID = @siteID );
START TRANSACTION;
    
    /* rendering the results as it goes so i have a visual trail of which
       Extended Attributes are being deleted, just in case. */
    SELECT *
    FROM tclassextendsets
    WHERE name = @extendSetName;
    
    SELECT tclassextendattributes.name, tclassextenddata.attributeValue
    FROM tclassextenddata
    LEFT OUTER JOIN tclassextendattributes
        ON tclassextenddata.attributeID = tclassextendattributes.attributeID
    WHERE tclassextendattributes.extendSetID = @extendSetID;
    
    
    DELETE FROM tclassextenddata
    WHERE attributeID IN ( SELECT attributeID FROM tclassextendattributes WHERE extendSetID = @extendSetID );
    DELETE FROM tclassextendattributes
    WHERE extendSetID = @extendSetID;
    DELETE FROM tclassextendsets
    WHERE extendSetID = @extendSetID;
COMMIT;

Basically the Extended Attributes are stored in 3 database tables. We have “tclassextendsets” that just stores the name of that group of Attributes.  Below that are “tclassextendattributes” and “tclassextenddata” that store the name and values of those Attributes respectively.  So “working backwards” from the child tables and then “deleting upwards” as you go, you can remove the bad Extended Attributes from Mura.

The snippet above is specific to MySQL, but it should mostly work on other databases with pretty minor changes to the “SET” and “START TRANSACTION” statements.  All of the SELECT and DELETE statements should work as-is (I think).

Of course, you should probably make a backup before deleting data from the database! Mura’s “Create Site Bundle” feature should archive everything you need, just in case.

-nolan