logo

The process and scripts for database cleaning

Often a lot of garbage is generated during the implementation of the system or a process, for example, process instances, documents, folders, messages, etc.

In order not to accumulate unnecessary data, it is recommended to conduct tests on a test server. Unfortunately, it is not always possible. ELMA allows you to mass-delete objects using scripts. In this article, we describe scripts for searching and removing various objects.

Warning 
The following scripts and the process must be used carefully, removing objects and interrupting processes are irreversible operations. We will not be held responsible for any consequences that may arise as a result of use of the information provided in this article.

Process

There are three branches in the process:

  • Searching and archiving documents
  • Searching and interrupting processes
  • Searching and removing folders

 The process can be divided into 4 steps

  1. Select action
  2. Enter search parameters
  3. View the list of the matched objects
  4. Confirm removal 
  1. Select Action 

It´s necessary to select a process branch 

  1. Enter search parameters

 Enter search parameters for the object. For example, an instance name. 

  1. View the list of the matched objects 
  1. Confirm removal 

The variable "Processes to interrupt" stores the names of all the processes that match the search criteria. View the list of the found items and either remove them or go back to the previous step. 

  1. Select Action 

The selected processes were interrupted, now you can complete the process or go to the first task. 

The process import file is attached to the article.  

Scripts

This process uses six scripts, two for each branch. All scripts are almost identical. The first script in each branch finds objects according to the specified search criteria and writes their names in a variable. The second script searches for the object, in the same way, removes them and then clears the variable with the search results.

Warning 
It is not recommended to make changes in these scripts, or use them separately from the main process. Use these scripts independently from the process only if you have experience in programming.

Searching and archiving documents

public void FindDocuments(Context context)
{
    //creating filters
    var docFilter = InterfaceActivator.Create<DocumentFilter>();
    // if the variable is not empty, add the document name in the filter 
    if(context.DocumentName != null)
    {
        docFilter.Name = context.DocumentName;
    }
    //and the folder
    if(context.Folder != null)
    {
        docFilter.Folder = context.Folder;
    }
    //and the authors
    if(context.Author != null)
    {
        foreach(var item in context.Author)
        {
            docFilter.CreationAuthor.Add((IUser)item);
        }
    }
    // Find documents that match the created filter 
    var foundDocuments = DocumentManager.Instance.Find(docFilter, null);
    //write the document names to variable 
    for each(var item in foundDocuments)
    {
        context.DocumentsToDelete += item.Name + Environment.NewLine;
    }
}
 
public void ToArchiveDocuments(Context context)
{
    //similar to the previous script 
    var docFilter = InterfaceActivator.Create<DocumentFilter>();
    if(context.DocumentName != null)
    {
        docFilter.Name = context.DocumentName;
    }
 
    if(context.Folder != null)
    {
        docFilter.Folder = context.Folder;
    }
 
    if(context.Author != null)
    {
        foreach(var item in context.Author)
        {
            docFilter.CreationAuthor.Add((IUser)item);
        }
    }
    var foundDocuments = DocumentManager.Instance.Find(docFilter, null).ToList();
    //forward the documents that match the search criteria to the archive  
    DocumentManager.Instance.Archive(foundDocuments);
    //clear the variable for reuse
    context.DocumentsToDelete = null;
}

Searching and interrupting processes 

public void FindProcess(Context context)
{
    //get current authenticated user 
    var user = AuthenticationService.GetCurrentUser<IUser>();
    //create filter for the process instance 
    var filter = InterfaceActivator.Create<IWorkflowInstanceFilter>();
    //status - current
    List<WorkflowInstanceStatus> statuses = new List<WorkflowInstanceStatus>();
    statuses.Add(WorkflowInstanceStatus.Running);
    filter.Statuses = statuses;
    //instance name
    filter.Name = context.InstanceName;
    // Find all instances matching the filter, excluding items matching the current process - the process cannot interrupt itself 
    var processForTerminate = WorkflowInstanceManager.Instance.Find(filter, null).Where(f => f.Name != context.WorkflowInstance.Name);
    //add matching items to the variable 
    foreach(var item in processForTerminate)
    {
        context.ProcessesToInterrupt += item.Name + Environment.NewLine;
    }
}
 
public void TerminateProcess(Context context)
{   //similar to the previous scripts  
    var user = AuthenticationService.GetCurrentUser<IUser>();
    var filter = InterfaceActivator.Create<IWorkflowInstanceFilter>();
    List<WorkflowInstanceStatus> statuses = new List<WorkflowInstanceStatus>();
    statuses.Add(WorkflowInstanceStatus.Running);
    filter.Statuses = statuses;
    filter.Name = context.InstanceName;
    var processForTerminate = WorkflowInstanceManager.Instance.Find(filter, null).Where(f => f.Name != context.WorkflowInstance.Name);
    // interrupt matching instances 
    foreach(var item in processForTerminate)
    {
        WorkflowInstanceManager.Instance.Terminate(item, "Automatic interruption", user);
    }
    context.ProcessesToInterrupt = null;
}

Searching and removing  folders

public void FindFolders(Context context)
{
    //create filter for the records management objects 
    var folderFilter = InterfaceActivator.Create<DmsObjectFilter>();
    folderFilter.Name = context.FolderName;
    var folders = FolderManager.Instance.Find(folderFilter, null);
    // Only one of the filter parameters will be applied to the folder. By default it´s the folder name. 
    if(context.FolderName != null)
    {
        foreach(var item in folders)
        {
            context.DeleteList += item.Name + Environment.NewLine;
        }
    }
    //if the folders are specified manually, add these folders 
    if(context.FoldersToDelete.Count > 0)
    {
        foreach(var item in context.FoldersToDelete)
        {
            context.DeleteList += item.Name + Environment.NewLine;
        }
    }
    //same for the ID folder
    if(context.FolderID != null)
    {
        context.DeleteList += FolderManager.Instance.LoadOrNull((long)context.FolderID).Name;
    }
 
}
 
public void DeleteFolders(Context context)
{
    //similar to the previous 
    var folderFilter = InterfaceActivator.Create<DmsObjectFilter>();
    folderFilter.Name = context.FolderName;
    var folders = FolderManager.Instance.Find(folderFilter, null);
    if(context.FolderName != null)
    {
        foreach(var item in folders)
        {
            FolderManager.Instance.Delete(item);
        }
    }
    if(context.FoldersToDelete.Count > 0)
    {
        foreach(var item in context.FoldersToDelete)
        {
            FolderManager.Instance.Delete(item);
        }
    }
     
    if(context.FolderID != null)
    {
        FolderManager.Instance.LoadOrNull((long)context.FolderID).Delete();
    }
    context.DeleteList = null;
}

 

Attachments