cancel
Showing results for 
Search instead for 
Did you mean: 

Automatically Deleting documents after XX days

bertlitman
Champ in-the-making
Champ in-the-making
Our users are requesting the ability for documents(placed in a certain Share document library folder) to be automatically deleted 90 days after they are placed in the repository.  How does one go about doing this?

Thank you.
11 REPLIES 11

jpotts
World-Class Innovator
World-Class Innovator
The short answer is to use a scheduled action:
http://wiki.alfresco.com/wiki/Scheduled_Actions

For what you need, you would configure your scheduled action with a cron statement, a query that finds the documents in the folder that are over 90 days old, and an action that deletes the documents.

You should be able to piece together your Lucene query by looking at examples on the Scheduled Actions wiki page. If not, post a reply back here.

The action you want to execute is the "execute-script" action. You'll tell it to run a piece of JavaScript that resides in "Data Dictionary/Scripts". That JavaScript will use the JavaScript API to delete the document which is just one line:

document.remove()

So, when the scheduled action wakes up, it will fire off the Lucene query to find the old docs, and then it will run the JavaScript against the docs it finds.

Jeff

landerigeno
Champ in-the-making
Champ in-the-making
Hello:
I also have to create a schedule action that delete all documents older than two days but I can't find the correct sintax for the query, can you help me. I post the whole action
<bean id="findoldfiles" class="org.alfresco.repo.action.scheduled.CronScheduledQueryBasedTemplateActionDefinition">
        <property name="transactionMode">
            <value>UNTIL_FIRST_FAILURE</value>
        </property>
        <property name="compensatingActionMode">
            <value>IGNORE</value>
        </property>
        <property name="searchService">
            <ref bean="SearchService"/>
        </property>
        <property name="templateService">
            <ref bean="TemplateService"/>
        </property>
        <property name="queryLanguage">
            <value>lucene</value>
        </property>
        <property name="stores">
            <list>
                <value>workspace://SpacesStore</value>
            </list>
        </property>    
        <property name="queryTemplate">
            <value>@cm\:created:${luceneDateRange(-P20D, -P2D)}</value>
        </property>
        <property name="cronExpression">
            <value>0 0/1 * * * ?</value>
        </property>
        <property name="jobName">
            <value>jobA</value>
        </property>
        <property name="jobGroup">
            <value>jobGroup</value>
        </property>
        <property name="triggerName">
            <value>triggerA</value>
        </property>
        <property name="triggerGroup">
            <value>triggerGroup</value>
        </property>
        <property name="scheduler">
            <ref bean="schedulerFactory"/>
        </property>
        <property name="actionService">
            <ref bean="ActionService"/>
        </property>
        <property name="templateActionModelFactory">
            <ref bean="templateActionModelFactory"/>
        </property>
        <property name="templateActionDefinition">
            <ref bean="deleteoldfiles"/>
        </property>
        <property name="transactionService">
            <ref bean="TransactionService"/>
        </property>
        <property name="runAsUser">
            <value>System</value>
        </property>
    </bean>

    <bean id="deleteoldfiles" class="org.alfresco.repo.action.scheduled.SimpleTemplateActionDefinition">
        <property name="actionName">
            <value>script</value>
        </property>
        <property name="parameterTemplates">
            <map>
                <entry>
                    <key>
                        <value>script-ref</value>
                    </key>
                    <value>${selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/cm:Record_x0020_Management/cm:delete.js"' )}</value>
                </entry>
            </map>
        </property>
        <property name="templateActionModelFactory">
            <ref bean="templateActionModelFactory"/>
        </property>
        <property name="dictionaryService">
            <ref bean="DictionaryService"/>
        </property>
        <property name="actionService">
            <ref bean="ActionService"/>
        </property>
        <property name="templateService">
            <ref bean="TemplateService"/>
        </property>
    </bean>


Thank you

Hi

Can you tell me the javascript code for this? My javascript is not working it is showing no nodes selected.

Thanks
Nancy

zladuric
Champ on-the-rise
Champ on-the-rise
It would be easier if you show us your code, maybe there are subtle errors we could help you spot?

My javascript code is as below


function addMonths(date, months) {
  date.setMonth(date.getMonth() + months);
  return date;
}


var docs = search.luceneSearch("PATH:\"/app:company_home/st:sites//*\" AND @cm\\:modified:[addMonths(new Date(), -6)]");

for(var dest : docs)
dest.move(C:\Alfresco\alf_data\archive)



and
scheduled-action-services-context.xml


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>
   
    <!–
    Define the model factory used to generate object models suitable for use with freemarker templates.
    –>
    <bean id="templateActionModelFactory" class="org.alfresco.repo.action.scheduled.FreeMarkerWithLuceneExtensionsModelFactory">
        <property name="serviceRegistry">
            <ref bean="ServiceRegistry"/>
        </property>
    </bean>
   
<!– run the script–>
<bean id="runscript" class="org.alfresco.repo.action.scheduled.CronScheduledQueryBasedTemplateActionDefinition">

        <property name="transactionMode">

            <value>ISOLATED_TRANSACTIONS</value>

        </property>

        <property name="compensatingActionMode">

            <value>IGNORE</value>

        </property>

        <property name="searchService">

            <ref bean="SearchService"/>

        </property>

        <property name="templateService">

            <ref bean="TemplateService"/>

        </property>

        <property name="queryLanguage">

            <value>lucene</value>

        </property>

        <property name="stores">

            <list>

                <value>workspace://SpacesStore</value>

            </list>

        </property>    

        <property name="queryTemplate">

            <value>PATH:"/app:company_home/st:sites"</value>

        </property>

        <property name="cronExpression">

            <value>0 0/15 * * * ?</value>

        </property>

        <property name="jobName">

            <value>jobA</value>

        </property>

        <property name="jobGroup">

            <value>jobGroup</value>

        </property>

        <property name="triggerName">

            <value>triggerA</value>

        </property>

        <property name="triggerGroup">

            <value>triggerGroup</value>

        </property>

        <property name="scheduler">

            <ref bean="schedulerFactory"/>

        </property>

        <property name="actionService">

            <ref bean="ActionService"/>

        </property>

        <property name="templateActionModelFactory">

            <ref bean="templateActionModelFactory"/>

        </property>

        <property name="templateActionDefinition">

            <ref bean="moveoldfiles"/>

        </property>

        <property name="transactionService">

            <ref bean="TransactionService"/>

        </property>

        <property name="runAsUser">

            <value>System</value>

        </property>

    </bean>

<!–Execute the script–>

    <bean id="moveoldfiles" class="org.alfresco.repo.action.scheduled.SimpleTemplateActionDefinition">

        <property name="actionName">

            <value>script</value>

        </property>

        <property name="parameterTemplates">

            <map>

                <entry>

                    <key>

                        <value>script-ref</value>

                    </key>

                    <value>${selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:archive.js"' )}</value>

                </entry>

            </map>

        </property>

        <property name="templateActionModelFactory">

            <ref bean="templateActionModelFactory"/>

        </property>

        <property name="dictionaryService">

            <ref bean="DictionaryService"/>

        </property>

        <property name="actionService">

            <ref bean="ActionService"/>

        </property>

        <property name="templateService">

            <ref bean="TemplateService"/>

        </property>

    </bean>
   </beans>

In my log file it is showing no nodes selected

The problematic instruction is
${selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:archive.js"' )} [on line 1, column 1 in string://fixed]

Please help

Thanks
Nancy

mitpatoliya
Star Collaborator
Star Collaborator
Hi Nancy
First problem I see in your xml is I think you have not checked this note
<!– Note - FreeMarker ${..} entries must be escaped in Spring context files –>

in this line

<value>${selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:archive.js"' )}</value>


you need to escape special charactes like "$" , "{" and "}"
so it would be like this


<value>\$\{selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:archive.js"' )\}</value>

Hi Mits

After making the above changes still my documents are not moving from the document library.

In log file it is showing error

ERROR [org.quartz.core.JobRunShell] Job jobGroup.jobA threw an unhandled Exception:
org.alfresco.error.AlfrescoRuntimeException: 05240000 SOLR queries can not be executed while the repository is bootstrapping

Nancy

mitpatoliya
Star Collaborator
Star Collaborator
well, this is because it is getting executed during bootstrap also and that is because your time duration in cron expression will executed it at each 15 min like 0,15,30 so on. Log which you see is related to first execution,But still if it should work properly during the second run when the server is started fully.

For that i have changed my cron expression to 0 14 12 1 * ? *. Still my documents are not moving.
In alfresco.log there are no errors but in alfrescotomcat-stdout.log it is showing error:

ERROR [solr.tracker.CoreTracker] [SolrTrackerScheduler_Worker-3] First transaction was not found with the correct timestamp.
2013-06-24 12:10:06,081  ERROR [solr.tracker.CoreTracker] [SolrTrackerScheduler_Worker-3] SOLR has successfully connected to your repository  however the SOLR indexes and repository database do not match.
2013-06-24 12:10:06,081  ERROR [solr.tracker.CoreTracker] [SolrTrackerScheduler_Worker-3] If this is a new or rebuilt database you SOLR indexes also need to be re-built to match the database.
2013-06-24 12:10:06,081  ERROR [solr.tracker.CoreTracker] [SolrTrackerScheduler_Worker-3] You can also check your SOLR connection details in solrcore.properties.
2013-06-24 12:10:06,081  ERROR [solr.tracker.CoreTracker] [SolrTrackerScheduler_Worker-3] Tracking failed
org.alfresco.error.AlfrescoRuntimeException: 05240000 Initial transaction not found with correct timestamp
   at org.alfresco.solr.tracker.CoreTracker.checkRepoAndIndexConsistency(CoreTracker.java:1523)


Nancy