autodelete folder content every night

cancel
Showing results for 
Search instead for 
Did you mean: 
TheCondor
Active Member

autodelete folder content every night

Hi, i'm on Community version 6.2 and i'm new to Alfresco at all. I just need for the simplest way for delete the content of a specific folder every night.

These folder is just a temporary passtrough, often users left files here which are useless...

Thanks in advance for any help!

2 Replies
EddieMay
Alfresco Employee

Re: autodelete folder content every night

Hi @TheCondor,

Welcome to Alfresco!

You might find this thread useful. This blog post is also worth reading. There is also this thread, that suggests a scheduled action.

You could also use the API to delete the folder & all its children - you could have a script that is run as a scheduled job & it would have to be run under admin user I believe. The "permanent=false" flag puts the content into the trashcan.

https://api-explorer.alfresco.com/alfresco/api/-default-/public/alfresco/versions/1/nodes/123?permanent=true

 To delete only the children, you could use this API call:

https://api-explorer.alfresco.com/alfresco/api/-default-/public/alfresco/versions/1/nodes/q/secondary-children/q

HTH,

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!
abhinavmishra14
Advanced

Re: autodelete folder content every night

When you say "way for delete the content of a specific folder every night",

You can write a scheduled job that will run on schedule and deletes the files in the configured folder. 

Refer these docs to learn more on scheduled jobs:

https://docs.alfresco.com/6.2/references/dev-extension-points-scheduled-jobs.html

https://hub.alfresco.com/t5/alfresco-content-services-blog/creating-scheduled-job-in-alfresco/ba-p/2...

Your scheduled job could be something like:

public class FolderCleanupJobProcessor extends AbstractScheduledLockedJob {

	private NodeService nodeService;

	private SearchService searchService;

	private TransactionService transactionService;
	
	private String deleteNodesSearchQuery;
	
	@Override
	public void executeJob(final JobExecutionContext jobCtx)
			throws JobExecutionException {
		try {
			// Run as system user since this job is user independent hence
			// permission is required on repository
			AuthenticationUtil.setRunAsUserSystem();
			//Create a method to get the file nodes from the configured folder
			//getListOfFilesFromArchiveFolder method can execute a search query and fetch all the nodes from the configured folder for deletion.
			final List<NodeRef> toBeDeletedNodes = getListOfFilesFromArchiveFolder();
			if (null != toBeDeletedNodes && !toBeDeletedNodes.isEmpty()) {
				for (final NodeRef nodeRef : toBeDeletedNodes) {
					final RetryingTransactionCallback<Object> txnWork = new RetryingTransactionCallback<Object>() {
						public Object execute() throws Exception {
							// Delete the nodeRef
							if (nodeService.exists(nodeRef)) {
								nodeService.deleteNode(nodeRef);
							}
							return null;
						}
					};
					transactionService.getRetryingTransactionHelper().doInTransaction(txnWork);	
				}
			}
			
			
		} catch (Exception excp) {
			//LOG Errors 
		}
	}
	
	//This method can execute a search query and fetch all the nodes from the configured folder for deletion.
	private List<NodeRef> getListOfFilesFromArchiveFolder() {
		List<NodeRef> nodeRefList = null;
		ResultSet results = null;
		try {
			//deleteNodesSearchQuery is for example: PATH:"/app:company_home/app:dictionary/cm:yourFolderName//*"
			//considering your folder is in /Company Home/Data Dictionary/ folder. You can adjust the search query depending on the location of your archive folder.
			//if your folder is in a site then, query could be something like: PATH:"/app:company_home/st:sites/cm:test-site/cm:documentLibrary/cm:yourFolderName//*"
			//in the above query, cm:test-site is the site short name.
			results = searchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_LUCENE, deleteNodesSearchQuery);
			if (null != results) {
				nodeRefList = results.getNodeRefs();
			}
		} finally {
			if (results != null) {
				results.close();
			}
		}
		return nodeRefList;
	}

	public void setNodeService(final NodeService nodeService) {
		this.nodeService = nodeService;
	}

	public void setSearchService(final SearchService searchService) {
		this.searchService = searchService;
	}
	
	public void setTransactionService(final TransactionService transactionService) {
		this.transactionService = transactionService;
	}
	
	public void setDeleteNodesSearchQuery(final String deleteNodesSearchQuery) {
		this.deleteNodesSearchQuery = deleteNodesSearchQuery;
	}
}

 [EDIT]: @EddieMay already provided useful stuff. Above response is additonal.

~Abhinav
(ACSCE, AWS SAA, Azure Admin)