Delete Liferay Web Content Articles Based on Structure ID with Groovy Script

Author: Ajay Choudhary

Liferay Version: 7.3, 7.4

If you need to delete all web content articles associated with a specific structure ID in Liferay using Groovy script, follow this guide.

Deleting Web Content Articles by Structure ID Groovy script

To delete articles associated with a specific structure ID, use the following Groovy script:

import com.liferay.journal.service.JournalArticleLocalServiceUtil

// Replace 12345 with your structure ID
long structureId = 12345L

// Fetch all journal articles
List articles = JournalArticleLocalServiceUtil.getJournalArticles(-1, -1)

// Filter articles by structure ID
List articlesToDelete = articles.findAll { it.getDDMStructureId() == structureId }

// Delete matching articles
articlesToDelete.each { article ->
    try {
        JournalArticleLocalServiceUtil.deleteJournalArticle(article)
        println "Deleted article: ${article.getTitleCurrentValue()} (ID: ${article.getArticleId()})"
    } catch (Exception e) {
        println "Failed to delete article: ${article.getTitleCurrentValue()} (ID: ${article.getArticleId()}), Error: ${e.message}"
    }
}

println "Total articles deleted: ${articlesToDelete.size()}"

Steps to Execute

  1. Open Control Panel > Server Administration > Script.
  2. Paste the script into the console.
  3. Click Execute.
  4. The console will display the titles and IDs of deleted articles and any errors encountered.

Best Practices and Warnings

Also Read: Count Web Content Articles by Structure ID in Liferay using Groovy script

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

close
Thanks !

Thanks for sharing this, you are awesome !