frank1 has asked for the wisdom of the Perl Monks concerning the following question:

Am trying to delete files uploaded 1 hour below, on Dropbox but am getting this error. even tho i have files uploaded below 1 hour ago

No files found from the last hour to delete.

#!/usr/bin/perl use strict; use warnings; use DateTime; use JSON; use WebService::Dropbox; my $Token_id = ''; my $key = ''; my $secret = ''; # Initialize Dropbox client my $dropbox = WebService::Dropbox->new({ key => $key, secret => $secret, }); $dropbox->access_token($Token_id); # Calculate the timestamp from one hour ago my $one_hour_ago = DateTime->now()->subtract(hours => 1); my @files_to_delete; # List files from a specific folder # Use "" for root dir my $result = $dropbox->list_folder({ path => "" }); foreach my $file_metadata (@{ $result->{entries} }) { if ($file_metadata->{'.txt'} eq 'file') { # Parse the file's timestamp my $file_modified_dt = DateTime->from_iso8601($file_metadata-> +{client_modified}); # Check if the file was modified within the last hour if ($file_modified_dt > $one_hour_ago) { push @files_to_delete, $file_metadata->{path_display}; } } } if (@files_to_delete) { # Delete the collected files in a batch $dropbox->delete({ entries => \@files_to_delete }); print "Deleted " . scalar(@files_to_delete) . " files uploaded in +the last hour.\n"; } else { print "No files found from the last hour to delete.\n"; }

Replies are listed 'Best First'.
Re: DropBox Delete files
by 1nickt (Canon) on Dec 27, 2025 at 12:25 UTC

    The documentation for WebService::Dropbox shows the way to delete a file is my $result = $dropbox->delete($path);. It does not show any support for the /2/files/delete_batch Dropbox API endpoint which does take {"entries":[{"path":"/foo/bar.txt"}]}.

    So you would need to integrate to the Dropbox API yourself and consume the /delete_batch endpoint, and then you would have to get the payload right (not just a list of path_display values).

    Or, if you want to use the Perl module, you'd have to loop through the list of files yourself.

    UPDATE:
    I forked the module and raised a pull request implementing delete_batch, see https://github.com/s-aska/p5-WebService-Dropbox/pull/22/commits/79cfc8edd0fd86e9e1424da0b455e119498e394c ... you could copy my changes to your local installation if the author does not respond quickly ...

    Hope this helps.


    The way forward always starts with a minimal test.

      Thanks for your idea, i decided to just loop through files to delete 1 by 1. and its working very good

      Thanks so much

      # Delete the collected files through loop foreach my $file_todelete (@files_to_delete) { $dropbox->delete($file_todelete); }
Re: DropBox Delete files
by Corion (Patriarch) on Dec 26, 2025 at 19:35 UTC

    What steps have you done to find out where your script goes wrong?

    You should look at what $result->{entries} contains, and also check the timestamps of these files manually.

    You should look at what $one_hour_ago contains and compare that against $file_modified_dt.

    We cannot do these steps for you.

      I have made changes step by step. and clear some errors, and the script outputs good, but when i check the files if deleted. there still there yet i got script on bottom saying

      Files and folders in '': Deleted 6 files uploaded in the last hour.

      my $access_token = ''; my $key = ''; my $secret = ''; # Initialize Dropbox client my $dropbox = WebService::Dropbox->new({ key => $key, secret => $secret, }); $dropbox->access_token($Token_id); # Specify the folder path you want to list. Use "" for the root folder +. my $folder_path = ""; # Call the list_folder method my $result = $dropbox->list_folder($folder_path); # Check for errors if ( $result->{'.sql'} eq 'error' ) { die "Error: " . Dumper($result); } my @files_to_delete; # Print the file and folder names print "Files and folders in '$folder_path':\n"; foreach my $entry ( $result->{entries}->@* ) { my $type = $entry->{'.txt'}; # 'file' or 'folder' my $name = $entry->{name}; my $path = $entry->{path_display}; my $server_modified = $entry->{server_modified}; # Define the format pattern (Z means UTC/GMT time zone) my $format_pattern = "%Y-%m-%dT%H:%M:%SZ"; # Parse the string into a Time::Piece object my $time_obj = Time::Piece->strptime($server_modified, $format_pat +tern); # Extract the hour (returns a two-digit string, e.g., "17") my $hour = $time_obj->hour; # Get the current time in UTC # We use gmtime() because the input timestamp is in UTC ('Z' suffi +x) my $t2 = gmtime; # Calculate the difference in seconds my $diff_seconds = $t2 - $time_obj; # Convert the difference to total hours (as a floating-point numbe +r) my $diff_hours = $diff_seconds / ONE_HOUR; # printf("File $name uploaded %d hours, %d minutes, %d seconds ag +o\n", $diff_seconds / ONE_HOUR, ($diff_seconds % ONE_HOUR) / ONE_MINU +TE, $diff_seconds % ONE_MINUTE); # prints File file1.txt uploaded 3 hours, 16 minutes, 48 seconds a +go # Check if the file was modified within the last hour if ($diff_hours > 1) { push @files_to_delete, $entry->{path_display}; } } if (@files_to_delete) { # Delete the collected files in a batch $dropbox->delete({ entries => \@files_to_delete }); print "Deleted " . scalar(@files_to_delete) . " files uploaded in +the last hour.\n"; } else { print "No files found from the last hour to delete.\n"; } # Printed Files and folders in '': Deleted 6 files uploaded in the las +t hour.