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
- Open Control Panel > Server Administration > Script.
- Paste the script into the console.
- Click Execute.
- The console will display the titles and IDs of deleted articles and any errors encountered.
Best Practices and Warnings
- Backup Data: Ensure you have a recent backup of your database before executing delete operations.
- Test in Development: Always test scripts in a development or staging environment first.
- Restrict Access: Limit access to the Script Console to trusted administrators only.
Also Read: Count Web Content Articles by Structure ID in Liferay using Groovy script