Plugin to Delete File After Print?


#1

Is there a plugin, or any way really, to delete the gcode file from Octoprint after a successful print in that option is selected? I usually print from Cura directly, and the file folder on OctoPrint gets full. I know I can delete the files manually from within OctoPrint, but it is SLOW as Christmas and I can only do one at a time. I tried installing the File Manager plugin and it's no better for this...


#2

Could probably do it with an event handler. Or somebody could add an item to the Cura plugin itself to delete the file.


#3

An event is definitely the way to go. See http://docs.octoprint.org/en/master/events/ for info and examples.


#4

Thanks guys. I was really hoping for a ready made solution, but... I shall dive into that link and see where the waters take me :slight_smile:


#5

I vote for this also. I tried as hard as I could to make an Event to do this. Don't even want to post it here to not confuse future readers. SOMEONE please write 5-6 lines what we could copy-paste in Yaml without breaking it.

Thanks!


#6

After thinking about it, while the "events" solution would work, it's not perfect in that it would delete EVERY print upon success rather than allowing you the option to decide whether or not to delete the print...


#7

I still vote for it and ask for help. to have something like this as an option. if my print fails I can reupload the file. and fortunately this happens not to often. on the other side I am printing hundreds of small files, and the list of files in the upload folder growing to the scary level.
Ideally this could be some sort of toggle in the UI, so you dont have to mess with config.yaml shall you need to maintain your files.


#8

add

rm -rf /home/pi/.octoprint/uploads/*

in the GCODE System Commands plugin.

then add a

OCTO112

at the end of you gcode.

maybe in a if/else structure if your printer support it.
Then you will be asked if the print was ok and the files only deleted when you say yes.


#9

it looks like this made my day.

although, how does it know which file to remove? or will it remove all of the files in uploads?

is there a way to send a system command to another octoprint instance on my network?

this would be a cool way for the printer who finished printing the file say to all the other printers "Hey I am done printing this file, take it away from your que" (I have all of my gcode files sent to all of my printers to be able to print any-one of them from any machine)


#10

it will delete all of the files
but with a shellscript you could check which file was last uploaded as an example and then just delete the last uploaded file.


#11

would:

-rf /home/pi/.octoprint/uploads/{name}

work to delete the currently loaded file?

http://docs.octoprint.org/en/master/events/


#12

I also like the idea to delete files after print, but the mentioned solution with the "Command Definitions" is in my opinion not user friendly enough.
I thought about something like the AutomaticShutdown-Plugin.
A simple checkbox "Delete after succesful print", add a listener for the succesful print event (Event: PrintDone) and delete the file.
If an other event occours, like "PrintFailed" nothing is deleted.

I think this is really easy to implement. I will start developing this weekend.
BR
Olli


#13

If you're planning a plugin, first you need to implement the EventHandlerPlugin mixin like so:

from octoprint.events import Events
class DeleteGcodePlugin(octoprint.plugin.EventHandlerPlugin):

Then you need to override on_event like so:

def on_event(self, event, payload):
  if event == Events.PRINT_DONE:
    # delete the gcode file here, it will need to be fetched from the print job I think
    printer_data = self._printer.get_current_data()
    current_job = printer_data.get("job", None)
    # I think the file name will now be in the current_job dict somewhere.

Obviously a little bit more needs to be done. If someone starts this I'll take a look.


#14

This is exactly what I was hoping for! I will be watching and hoping :slight_smile:


#15

Yes, its working. Thx @FormerLurker.
I added some other lines and now after the print is done the file is deleted.

        destination = payload.get("origin", "")
        path = payload.get("name", "")
        
        self._printer.unselect_file()
        self._file_manager.remove_file(destination, path)
        
        self._logger.info("File deleted.")

Next step is to implement the UI.


#16

Excellent! You work fast :slight_smile: Do you have a link to the repo? I would love to watch this evolve. Let me know if you need a tester.


#17

Done!!
The Plugin is available here: https://github.com/OllisGit/OctoPrint-DeleteAfterPrint

The PullRequest for the official Plugin-Repo is also created.

Please test and if you find an issue (bug/enhancement/question) please use the GitHub-IssuesTracker.
So that I can track each topic.

BR
Olli


#18

Very nice! Good to know that the file name is in the on_event payload. I also never thought about the difference between deleting from the sd card and locally. Also, good call adding a 'remember' setting so you can selectively delete after the print completes, or delete all completed files. Bravo.


#19

Dude! You are amazing! I will install this tonight and give it a go. Thank you!


#20

To be honest, I have not tested the sd-card deletion, because I don't use sd-cards.
The implementation is copied from the "offical" remove-action.