Deleting database entries using a script call.

Like the title says, is there a way to force a database entry to be completely deleted or replaced using a script call?

For those curious as to why I am looking to do this, in my game, Eternal Twilight, I've recreated an equipment system similar to that of Diablo / Borderlands, where all equipment that is acquired will be given random prefixes and suffixes. This is done using Yanfly's Item Core ( http://yanfly.moe/2015/10/16/yep-11-item-core/ ) and DreamX's Random prefix/suffix plugin ( https://forums.rpgmakerweb.com/index.php?threads/random-item-prefixes-suffixes.57646/ ).

This all works like a charm, buuuuuuuuuuuuuuut, there is a drawback to it. Every single item that is made gets a brand new database entry. After a few hours of playing my loot fest game, there are easily over 1k+ new entries added to the database, which is artificially bloating the hell out of the save files. By end-game, it's not uncommon to see save files that are 2mb in size or larger, which causes some pretty ugly loading/save times.

Periodically throughout the game, I want to do a "database purge" where I input a scriptcall that will mass-delete database entries that have been added by these plugins if said item is no longer in the player's inventory.

Thank you so much for any help or guidance you have!
Yanfly's independent items are located in $dataItems. Also, if you check the function DataManager.makeSaveContents, you can also see from where this data gets saved to savefiles. Since it's an array, if you want to delete only some entries, you may find the splice method useful.
I haven't worked with Yanflys plugins yet, and I've only skimmed through the plugin, so I can't say anything for absolutely certain:

The plugin seems to already have a way to clear independent items, and it already should at least clear database entries of items the player sells through the shop (see changes to Scene_Shop.prototype.doSell ).
Although this leaves behind an increasing number of "empty" entries, which should not take away too much space in your savefile.

Yanflys plugin seems to decide whether an item is still in use by checking the inventory and each actors equipment for it. The process may have to be modified if you are saving these items elsewhere.

Without taking any responsibility, something like this might work:
Code:
var removeUnusedItem = function(item) {
    DataManager.removeIndependentItem(item);
};
DataManager._independentWeapons.forEach(removeUnusedItem);
DataManager._independentArmors.forEach(removeUnusedItem);
@Another Fen - Thanks for the help! I've tested it and it definitely helps to decrease the save file bloat. I did run into an error that crashes the game when you go to load the file though. Let me pull up a screenshot of the console.

Spoiler
error.png

I'm not very well-versed in javascript myself, but my assumption, by looking at your code, is that the function is removing ALL independent items, even the ones in the party inventory and the stuff the party is wearing.

If there was a way to not delete independent items that the player still owns, then this code would be perfectly what I'm looking for.
The function actually includes a check whether the item is "used" by checking party inventory and equipment of all actors. However, that function does consider any item currently in the inventory unused, which at least on first glance looks like an oversight to me (when only using this function after selling a unique item, this would never be an issue anyways).
I might be wrong about that though.

However, unless you change the function you would indeed have to add the inventory check to your script call:
Code:
var removeUnusedItem = function(item) {
    if (!$gameParty.hasItem(item)) {
        DataManager.removeIndependentItem(item);
    }
};
DataManager._independentWeapons.forEach(removeUnusedItem);
DataManager._independentArmors.forEach(removeUnusedItem);
@Another Fen - You are my savior. It works like a charm. Thank you so so so so much! If there's any way that I can repay your kindness, let me know! 🙂

Original thread: https://forums.rpgmakerweb.com/threads/deleting-database-entries-using-a-script-call.103302/