Skip Navigation

[Resolved] Delete images from server after front-end CRED Form update

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 7 replies, has 2 voices.

Last updated by Luo Yang 1 year, 6 months ago.

Assigned support staff: Luo Yang.


Hi guys,

I know that this has been discussed before, and I got your logic about this being as expected and possibility of photos being used elsewhere, but please listen to me very carefully, because I can not express myself any clearer..

I NEED To cleanup the server from images that people uploaded to their profile(CPT), but then deleted via the front-end form, in order TO GROW MY PROJECTS!

Leaving those images on the server is unthinkable, because this means that I will have to keep adding storage capacity to my web server forever simply because my users use the platforms, even when the user base stops growing.

SO PLEASE, help me get this going as this is the only thing that prevents my projects of having hundreds of thousands of users. I can not even start advertising them, knowing that without getting file management right from the start I’m doomed to have messed up server that I will never be able to clean up. And as I see I’m not the only one having this issue.

Once again - I don’t care about WP logic, or photos being used elsewhere, because they are not!

- I have each user photos stored in their own folder (it has their ID as a name).
- I have limited users access to their own uploads only.
- And we’re talking about user profile(CPT) photos (and a repeating field as part if a gallery), so photos being used elsewhere is simply not true.

I want a fix for this *HUGE BUG* on front-end forms now, and I don’t care about if you see it as a bug or not, because I’m the client here, and I say that it is a bug, and a PETABYTE LEAK.

Please finally! Help me solve this issue and let me grow my all my projects now!


Luo Yang

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)


Thanks for the details, there isn't such kind of built-in feature within Toolset Forms plugin, as a workaround, you can try with custom codes, for example:
1) When user submit the post form, use action hook cred_save_data to trigger a PHP function:

2) In this PHP function:
- Get the images attached to current post,

- Get the images filled in your custom repeating image field

- Compare above two values, and find those unused image files, and remove them:


Hi Luo,

Thanks for your input on my issue.

I will test this. I will possibly have to also check for one photo being used as a profile photo, as this is a separate custom field.

Once ready, I will report back and share my code so that other users can use it.



Luo Yang

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

OK, please let me know if you need more assistance for it, thanks


Okay guys, this is for all the other Toolset users who waited for this function!

/* 10 Custom Action to Cleanup files from database and server upon submitting specific cred forms */
add_action('cred_save_data','images_cleanup', 10, 2);
function images_cleanup($post_id, $form_data) {
	# if a specific form or two..
	if (($form_data['id']==88) || ($form_data['id']==114)) {
	    $type = get_post_type($post_id, $form_data);
	    # if a specific post type..
	    if ($type == 'profile') {
	     	global $wpdb;
			# Compare both tables and remove duplicates while listing only the ID from wp_post as attachment
			$allposts = $wpdb->get_results( $wpdb->prepare("SELECT id FROM wp_posts WHERE post_parent=$post_id AND post_type ='attachment' AND guid NOT IN (SELECT meta_value FROM wp_postmeta WHERE post_id = $post_id AND meta_key LIKE 'wpcf-user-gallery' OR meta_key LIKE '_wp_attached_file' OR meta_key LIKE 'wpcf-user-photo')") );
			foreach ($allposts as $singlepost) { 
				wp_delete_attachment($post_id_n1, true);
			} # END global $wpdb;
	    } # END specific post type
	} # END specific form
  } # END function images_cleanup 

Make sure you update your form ID's (two forms because one is to Create the CPT, and the other is to Edit it) and your post type (mine is 'profile' here)!

You might also want to change your file structure in the uploads folder to have a separate folder with the user's ID, use this code:

/* Per user upload dir function */
	function per_user_upload_dir( $original ){
	    // use the original array for initial setup
	    $modified = $original;
	    // set our own replacements
	    if ( is_user_logged_in() ) {
	        $current_user = wp_get_current_user();
	        $subdir = $current_user->ID;
	        $modified['subdir'] = $subdir;
	        $modified['url'] = $original['baseurl'] . '/' . $subdir;
	        $modified['path'] = $original['basedir'] . DIRECTORY_SEPARATOR . $subdir;
	    return $modified;
	add_filter( 'upload_dir', 'per_user_upload_dir');


To Toolset developers:
- Please consider the file attachment cleanup option as an addition to your plugin functionality. Otherwise websites will become server hogs..


Luo Yang

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Thanks for sharing the codes, that will help other users.

And I will forward your request for our developers evaluation.


My issue is resolved now. Thank you!


Luo Yang

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Sorry for the typo, just corrected