# New API: Buffer.clear\_undo\_stack()

**URL:** <https://forum.sublimetext.com/t/new-api-buffer-clear-undo-stack/60561>\
**Category:** Plugin Development\
**Created:** [September 4, 2021, 1:03am UTC](https://forum.sublimetext.com/t/new-api-buffer-clear-undo-stack/60561 "2021-09-04T01:03:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MLR53](https://forum.sublimetext.com/user_avatar/forum.sublimetext.com/mlr53/32/75259_2.png) [@MLR53](https://forum.sublimetext.com/u/MLR53)\
**Post date:** [September 4, 2021, 1:03am UTC](https://forum.sublimetext.com/t/new-api-buffer-clear-undo-stack/60561/1 "2021-09-04T01:03:11Z")

</div>

== BUILD 4114 == 3 September 2021 ==

When closing a (multi-file) project I want to clear all undo data.  
I am assuming todays new API routine will do the trick.

on\_pre\_close\_project(window)  
Buffer.clear\_undo\_stack()

Please help with the code layout for this. I have demonstrated many other (py) working examples but nothing for Buffer.clear\_undo\_stack(). No matter what and after many hours, it always comes up undefined.

“c:\Users\markr\AppData\Roaming\Sublime Text\Packages\User\On\_Project\_Close\_Clear\_Buffers.py”

API Reference  
[http://www.sublimetext.com/docs/api\_reference.html#sublime.View:ver-dev](http://www.sublimetext.com/docs/api_reference.html#sublime.View:ver-dev)

---

<div class="post-metadata">

**Author:** ![bschaaf](https://forum.sublimetext.com/user_avatar/forum.sublimetext.com/bschaaf/32/70376_2.png) [@bschaaf](https://forum.sublimetext.com/u/bschaaf)\
**Post date:** [September 4, 2021, 1:35am UTC](https://forum.sublimetext.com/t/new-api-buffer-clear-undo-stack/60561/2 "2021-09-04T01:35:28Z")

</div>

Unfortunately the changelog had the wrong thing in it. It’s a method on `View`, not `Buffer`.

---

<div class="post-metadata">

**Author:** ![Yuserissublime](https://forum.sublimetext.com/letter_avatar_proxy/v4/letter/y/3ec8ea/32.png) [@Yuserissublime](https://forum.sublimetext.com/u/Yuserissublime)\
**Post date:** [September 4, 2021, 3:17am UTC](https://forum.sublimetext.com/t/new-api-buffer-clear-undo-stack/60561/4 "2021-09-04T03:17:32Z")

</div>

Can’t we do anything to prevent previous changes to undo after close S4 ?

---

<div class="post-metadata">

**Author:** ![MLR53](https://forum.sublimetext.com/user_avatar/forum.sublimetext.com/mlr53/32/75259_2.png) [@MLR53](https://forum.sublimetext.com/u/MLR53)\
**Post date:** [September 5, 2021, 12:08am UTC](https://forum.sublimetext.com/t/new-api-buffer-clear-undo-stack/60561/5 "2021-09-05T00:08:32Z")

</div>

Good to go. Close and reopen project to clear all file undo history.

```
import sublime
import sublime_plugin

class Clear_Undo_Stack(sublime_plugin.ViewEventListener):
    def on_load(self):
        self.view.clear_undo_stack()

# class clearundoCommand(sublime_plugin.TextCommand):
# def run(self, edit):
# self.view.clear_undo_stack()
# #
# # Warning: Can't clear undo stack from text command
# #
# #obj = self.view
# #for attr in dir(obj):
# # print("obj.%s = %r" % (attr, getattr(obj, attr)))
# #
# # Finds: obj.clear_undo_stack = <bound method View.clear_undo_stack of View(16)>
#
# Open the Python console (Ctrl+`)
# print('\n'*100) to clear screen
# Type: view.run_command("clearundo") and press enter.

```

**API Reference**  
[http://www.sublimetext.com/docs/api\_reference.html#ver-dev](http://www.sublimetext.com/docs/api_reference.html#ver-dev)

**Dev Builds** Changelog BUILD 4114 3 September 2021  
[http://www.sublimetext.com/dev](http://www.sublimetext.com/dev)

~~API: Added **Buffer**._clear\_undo\_stack()_~~

Please update with: **View**._clear\_undo\_stack()_
