SyncS3 for Gravity Forms allows you to send file uploads to any Amazon S3 bucket when your forms are submitted. Each time an entry is created, those files are sent to S3, and a reference to the file is saved in the entry on your website.
If you regularly find yourself deleting entries from your website, you may want to also delete the files that were sent to Amazon S3. If you don’t have a specific reason to keep the files, why pay for the space they take up?
By default, SyncS3 does not have a feature to automatically remove the files when an entry is deleted. This is by design, because we’d rather be safe and not remove files. Instead, you can elect to remove the files by using the following code snippet.
add_action( 'gform_delete_entry', 'syncs3_remove_s3_urls_on_delete_entry' );
/**
* Delete an entry's files from Amazon S3 when an entry is deleted.
* This fires before the entry is fully deleted, so the entry meta is still available.
*
* @param int $entry_id Entry ID to be deleted
*
* @return void
*/
function syncs3_remove_s3_urls_on_delete_entry( $entry_id ) {
$s3_urls = gform_get_meta( $entry_id, 's3_urls' );
if ( ! empty( $s3_urls ) ) {
foreach ( $s3_urls as $field_id => $urls ) {
if ( ! empty( $urls ) ) {
foreach ( $urls as $url ) {
if ( ! empty( $url['file_url'] ) ) {
syncs3_delete_file( $entry_id, syncs3_get_url_parts( $url['file_url'] )['file_name'] );
}
}
}
}
}
}
This code runs when an entry is deleted from your website’s database. Note that this is not the same as moving an entry to the trash. For this code to do its job – delete the files from S3 – you need to trash the entry, and then empty your trash.
Also, for this code to work, you need to be running SyncS3 Pro 1.2.0. Prior to 1.2.0, not enough information was saved about each file to remove them. Therefore, this will only work for deleting entries that were created on SyncS3 1.2.0 or later.