Is it possible to delete a snippet in php?

I need to make a One time use page that automatically deletes its self after it is accessed and all of the correct info is inputted. This is for onboarding new clients to their modx installs.

Yes you can delete a snippet.

Something like this should work: (untested)

$snippet = $modx->getObject('modSnippet', $snippet_id);
$snippet->remove();

So why do you want to delete a snippet and not a resource/page?

what would the code look like for deleting a template? Would it be ‘modTemplate’ or something like that? Also if I have a php file that was uploaded through sftp how would I call a system setting in that php file? Ive tried to use the $modx object but it doesn’t work.

Yes, “modTemplate” is the class for the templates.


If you want to have access to $modx, you have to load MODX externally:
https://docs.modx.com/current/en/extending-modx/modx-class/loading-externally

The code is somewhat different if you are using MODX 3.


Also after deleting a snippet you probably have to clear the cache ($modx->cacheManager->refresh();) or the snippet code will still exist in the cache.

1 Like

Thank you!!!

I’m almost certain that you don’t really want to delete a snippet. If I’m understanding you, you want a page that the user can’t access more than once.

If you can give us some more details, like how the user gets to the page, and what the page is for (registration, for example), and whether multiple users will go through the process, we might be able to suggest a better solution.