Selective delete based on last action

I’ve got a massive Archive and want to cull all the Drafts whose last action taken was “Add to Draft”. I don’t see anything in the Directory, so I presume this would involve writing a script that would loop through the actionLogs property, match something there against “Add to Draft”, then, as long as this is a reliable and correct way to identify it, delete the Draft. Am I on the right track? I’m not a JS whiz, so if anyone cares to get me started with a code snippet, that would be quite welcome!

Something like the below might work for you. It will assign a “to-delete” tag to the archived drafts with a matching action log for that action name. You could then filter the draft list by that tag to review and select them all to trash.

// this gets you all the drafts in the archive
let drafts = Draft.query("", "archive")

// loop over the drafts
for (let d of drafts) {
    // loop over each draft's action logs
    for (let log of d.actionLogs) {
        // if the action name matches, assign a tag to the draft
        if (log.action.name == "Add to Draft") {
            d.addTag("to-delete")
            d.update()
        }
    }
}
1 Like

Thanks, that worked, after a few tweaks…

// this gets you all the drafts in the archive
let drafts = Draft.query("", "archive");

// loop over the drafts
for (let d of drafts) {
	// If an action log exists...
	if (d.actionLogs) {
		// Loop over each draft's action log...
		for (let l of d.actionLogs) {
			// If the action exists...
			if (l.action) {
    			// If the action is the designated action...
    			if (l.action.name == "Add to Draft") {
    				// Tag the draft...
    				d.addTag("to-delete");
    				d.update();
    			}
    		}
    	}
	}
}
1 Like