Remove orphaned data, clean up test data, and optimize database performance
Data Cleanup Script
advanced high risk Data Management
Description
Prerequisites
- Database backup
- System maintenance window
- Administrator privileges
Parameters
cleanupType
String*Type of cleanup: orphaned, testdata, or performance
batchSize
NumberNumber of records to process in each batch
Default: 1000
confirmCleanup
Boolean*Confirm that cleanup should proceed
Script Code
import de.hybris.platform.servicelayer.search.FlexibleSearchService
import de.hybris.platform.servicelayer.model.ModelService
import de.hybris.platform.core.model.user.UserModel
def cleanupType = parameters.cleanupType
def batchSize = parameters.batchSize ?: 1000
def confirmCleanup = parameters.confirmCleanup
def errors = []
def cleanedCount = 0
if (!confirmCleanup) {
errors.add("Cleanup not confirmed. Set confirmCleanup to true to proceed.")
return
}
try {
switch (cleanupType) {
case 'orphaned':
// Clean up orphaned data
def orphanedQuery = """
SELECT {pk} FROM {Item}
WHERE {pk} NOT IN (
SELECT {source} FROM {Relation}
UNION
SELECT {target} FROM {Relation}
)
"""
def orphanedItems = flexibleSearchService.search(orphanedQuery).result
orphanedItems.each { item ->
try {
modelService.remove(item)
cleanedCount++
} catch (Exception e) {
errors.add("Error removing orphaned item $item.pk: $e.message")
}
}
break
case 'testdata':
// Clean up test data
def testUsers = flexibleSearchService.search("SELECT {pk} FROM {User} WHERE {uid} LIKE 'test%'").result
testUsers.each { user ->
try {
modelService.remove(user)
cleanedCount++
} catch (Exception e) {
errors.add("Error removing test user $user.uid: $e.message")
}
}
break
case 'performance':
// Performance optimization
println "Running performance optimization..."
// Add performance optimization logic here
cleanedCount = 1
break
}
println "Cleaned up $cleanedCount items successfully"
if (errors) {
println "Errors encountered:"
errors.each { println " - $it" }
}
} catch (Exception e) {
errors.add("Cleanup script failed: $e.message")
}Script Information
Author:SAP Commerce Team
Execution Time:5-15 minutes
Rating:4.3/5
Downloads:890
Last Updated:2024-01-10
Version:2.1.0
Tags
tools.groovyScriptLibrary.ui.title
tools.groovyScriptLibrary.ui.subtitle