# Remove all files from a Collection

**URL:** <https://resource.dopus.com/t/remove-all-files-from-a-collection/33061>\
**Category:** Help & Support\
**Created:** [July 12, 2019, 7:45pm UTC](https://resource.dopus.com/t/remove-all-files-from-a-collection/33061 "2019-07-12T19:45:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hoosh](https://resource.dopus.com/letter_avatar_proxy/v4/letter/h/ad7895/32.png) [@Hoosh](https://resource.dopus.com/u/Hoosh)\
**Post date:** [July 12, 2019, 7:45pm UTC](https://resource.dopus.com/t/remove-all-files-from-a-collection/33061/1 "2019-07-12T19:45:42Z")

</div>

Is it possible to quickly remove all files and folders from a collection (and from all Sub-Collections in that collection).  
I have a collection that is changing contents all the time (it's files I want to copy to my phone). Once I copy the files, I'd like a quick way to remove all the files and folders from the Collection, _but leave all the Sub-Collections_.

---

<div class="post-metadata">

**Author:** ![Zion](https://resource.dopus.com/user_avatar/resource.dopus.com/zion/32/44060_2.png) [@Zion](https://resource.dopus.com/u/Zion)\
**Post date:** [July 12, 2019, 8:08pm UTC](https://resource.dopus.com/t/remove-all-files-from-a-collection/33061/2 "2019-07-12T20:08:51Z")

</div>

This is one of my buttons. It deletes the existing collection and copies selected files/folders to a newly created Collection with the same collection name (as the deleted collection. So it deletes and recreates the same collection in one step and adds the selected files.

[Go 0\_Red.dcf](https://resource.dopus.com/uploads/short-url/hJ23Bim1LhKXVr7IwsxbVuOZMBQ.dcf) (668 Bytes)

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [July 12, 2019, 8:21pm UTC](https://resource.dopus.com/t/remove-all-files-from-a-collection/33061/3 "2019-07-12T20:21:32Z")

</div>

Unless the list of sub-collections is fixed, you'd probably need a script to clear everything from the collection and all sub-collections without also deleting the sub-collections themselves.

If the list of sub-collections is fixed then it's fairly easy.

Or you could have a button which deletes everything and then recreates new, empty (sub-)collections from a list of things you want created that would live in the button. That should also be quite simple.

---

<div class="post-metadata">

**Author:** ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)\
**Post date:** [July 12, 2019, 8:26pm UTC](https://resource.dopus.com/t/remove-all-files-from-a-collection/33061/4 "2019-07-12T20:26:02Z")

</div>

How about deleting with a filter?

```
Delete REMOVECOLLECTION FILTER NoCollection

```

With a filter `NoCollection` like this:

![image](https://resource.dopus.com/uploads/default/original/3X/1/9/1970b500fc8660d672369a351b330b6d292ca120.png)

(Quick shot with little testing)

---

<div class="post-metadata">

**Author:** ![Hoosh](https://resource.dopus.com/letter_avatar_proxy/v4/letter/h/ad7895/32.png) [@Hoosh](https://resource.dopus.com/u/Hoosh)\
**Post date:** [July 14, 2019, 3:08am UTC](https://resource.dopus.com/t/remove-all-files-from-a-collection/33061/5 "2019-07-14T03:08:22Z")

</div>

@Leo

> [@Leo](#):
>
> If the list of sub-collections is fixed then it's fairly easy.

Okay say the list of sub-collections is fixed. How could I do it?

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [July 14, 2019, 4:06pm UTC](https://resource.dopus.com/t/remove-all-files-from-a-collection/33061/6 "2019-07-14T16:06:38Z")

</div>

lxp's suggestion looks better, if that works. I hadn't thought of that.

---

<div class="post-metadata">

**Author:** ![Hoosh](https://resource.dopus.com/letter_avatar_proxy/v4/letter/h/ad7895/32.png) [@Hoosh](https://resource.dopus.com/u/Hoosh)\
**Post date:** [July 14, 2019, 4:35pm UTC](https://resource.dopus.com/t/remove-all-files-from-a-collection/33061/7 "2019-07-14T16:35:08Z")

</div>

> [@Leo](#):
>
> lxp's suggestion looks better, if that works. I hadn't thought of that.

It works, but it isn't recursive -- you have to go into each sub-collection, select all, and run the button.

---

<div class="post-metadata">

**Author:** ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)\
**Post date:** [July 14, 2019, 7:45pm UTC](https://resource.dopus.com/t/remove-all-files-from-a-collection/33061/8 "2019-07-14T19:45:58Z")

</div>

This script recursively removes all files and folders from a collection and its sub-collections:

```javascript
function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    cmd.RunCommand('Set UTILITY=otherlog');
    DOpus.ClearOutput;
    DOpus.Output('Items to be removed:\n');
    CleanCollection(tab.path);
    DOpus.Output('\n... done.');
}

function CleanCollection(coll) {
    var cmd = DOpus.Create.Command();
    var fsu = DOpus.FSUtil;
    var folderEnum = fsu.ReadDir(coll);
    while (!folderEnum.complete) {
        var fileItem = folderEnum.Next();
        if (String(fileItem.realpath).substr(0, 7) == 'coll:\/\/') {
            CleanCollection(fileItem.realpath);
        } else {
            var cmdLine = 'dopusrt \/col remove "' + coll + '" "' + fileItem.name + '"';
            DOpus.Output(cmdLine);
            // If you like the results, un-comment the following line
            // cmd.RunCommand(cmdLine);
        }
    }
}

```

[RemoveFilesAndFoldersFromCollections.dcf](https://resource.dopus.com/uploads/short-url/ztOxJrnkop4LLhPtdHZlG8jPQ8o.dcf) (2.1 KB)

Note that you have to edit the script to actually remove files and folders.

---

<div class="post-metadata">

**Author:** ![Hoosh](https://resource.dopus.com/letter_avatar_proxy/v4/letter/h/ad7895/32.png) [@Hoosh](https://resource.dopus.com/u/Hoosh)\
**Post date:** [July 15, 2019, 1:47am UTC](https://resource.dopus.com/t/remove-all-files-from-a-collection/33061/9 "2019-07-15T01:47:52Z")

</div>

@lxp  
Hey thanks a lot, that's awesome!  
Only problem is it doesn't work on files/folders with parentheses (maybe other wildcard characters too?).  
I'm no Javascript expert, but my solution to escape all DOpus's wildcard characters was after this line:

```javascript
var cmdLine = 'dopusrt \/col remove "' + coll + '" "' + fileItem.name + '"';

```

I added this line:

```javascript
cmdLine = cmdLine.replace(/(\[|\]|\(|\)|\'|\~|\#)/g,'\'$1');

```

[RemoveFilesAndFoldersFromCollections-wildcards-escaped.dcf](https://resource.dopus.com/uploads/short-url/gWduG5rl03EOVNzGcLm8bNr0g2Z.dcf) (2.0 KB)

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [July 15, 2019, 2:03am UTC](https://resource.dopus.com/t/remove-all-files-from-a-collection/33061/10 "2019-07-15T02:03:11Z")

</div>

We'll add a scripting method to escape wildcard characters in the next update, to make things easier.
