Delete a File from a Resource

This code snippet will show you how to delete a single file from a HydroShare rsource using the HydroShare API. This could be a useful function in the case where you need to replace a file in a resource with a newer updated version of the same resource. You could also use this function to allow your code to clean up a resource by deleting unneeded files. Keep in mind, that all deletions of files using this method are PERMENANT, so be careful ! But this feature works smoothly without any glitches, just try copying the resource id from the HTTP hyperlink when you open your resource in Hydroshare.

Our demonstration code here is comprised of two steps. Step 1 will query the resource and generate a list of files available therein. Step 2 will actually delete a selected file from a resource.

Step 1: Get a list of files in a resource from HydroShare:

In the boxes below you enter your HydroShare username and its password. If you have to make an HydroShare account, click here.

The resource Id is a unique identifier for every resource on Hydroshare. You can findit by browsing to your HydroShare resource copying the ID from the URL. Like for example, this is an example hyperlink from which the 32 letter combination at the end is called the resource id. And so the resource id from the example hyperlink is 08c6e88adaa647cd9bb28e5d619178e0

Resource ID

Step 2: Choose a file in the resource and delete it:

Learn more:

To try this on your own, you can use the code snippet below, or download the full javascript file that is used in this demonstration

        auth = HydroShareAuthBasic(username= username, password= password)
            hs = HydroShare(auth=auth)
            resource_id = hs.deleteResourceFile(resourcein, title)
            messages.error(request, "File deleted successfully")
        if has_errors:    
            messages.error(request, "Please fix errors.")