How to Remove "Working Copy Documents" from Alfresco

Remove working copy documents in Alfresco Explorer or Alfresco Share

Yesterday I've been asked to cancel all the working copy documents from alfresco sites.
So, I've used JavaScript API to cancel all the working copy documents.
Step 1
First, get all the node references of the working copy documents using search service.
Step 2
Use the nodeservice to cancel the working copy document.
  1. var results = search.selectNodes("workspace://SpacesStore""descendant::*[hasAspect('cm:workingcopy')]");  
  2."Total rows" + results.length);  
  3. for (var i = 0; i < results.length; i++) {  
  4.  var node = results[i];  
  5.  var nodename =;  
  6.  node.cancelCheckout();  
  7.  logger.error(nodename + " cancelled");  
  8. }