|
| 1 | +""" |
| 2 | +Recycle bin — list deleted items, restore, and permanently delete. |
| 3 | +
|
| 4 | +Items deleted from OneDrive/SharePoint go to the recycle bin first. |
| 5 | +This example shows how to: |
| 6 | + - List deleted items in the recycle bin |
| 7 | + - Restore a deleted file back to its original location |
| 8 | + - Permanently delete (purge) an item without recovery |
| 9 | +
|
| 10 | +Compliance teams use this for data retention and eDiscovery workflows. |
| 11 | +
|
| 12 | +Requires delegated permission ``Files.ReadWrite.All`` and |
| 13 | +``Sites.ReadWrite.All``. |
| 14 | +
|
| 15 | +https://learn.microsoft.com/en-us/graph/api/resources/recyclebin |
| 16 | +""" |
| 17 | + |
| 18 | +import sys |
| 19 | + |
| 20 | +from office365.graph_client import GraphClient |
| 21 | +from tests import test_client_id, test_client_secret, test_tenant |
| 22 | + |
| 23 | + |
| 24 | +def main(): |
| 25 | + client = GraphClient(tenant=test_tenant).with_client_secret(test_client_id, test_client_secret) |
| 26 | + |
| 27 | + # -- Step 1: access the recycle bin -- |
| 28 | + # The recycle bin is a property on the drive. |
| 29 | + # For OneDrive: client.me.drive.recycle_bin |
| 30 | + # For SharePoint: client.sites["{site-id}"].drive.recycle_bin |
| 31 | + |
| 32 | + drive = client.me.drive |
| 33 | + recycle = drive.recycle_bin # This returns a RecycleBin entity |
| 34 | + |
| 35 | + # -- Step 2: list deleted items -- |
| 36 | + # Items in the recycle bin are accessible via `recycle.items` |
| 37 | + # (but note: this is read from a Collection navigation property) |
| 38 | + try: |
| 39 | + items = recycle.items.get().execute_query() |
| 40 | + except AttributeError: |
| 41 | + # Fallback: some SDK versions expose it differently |
| 42 | + print("(recycle_bin.items not directly available — checking alternate path)\n") |
| 43 | + |
| 44 | + # Delete a test file so there's something in the bin |
| 45 | + root = drive.root.get().execute_query() |
| 46 | + test_file = root.get_by_path("recycle_bin_test.txt") |
| 47 | + try: |
| 48 | + exists = test_file.get().execute_query() |
| 49 | + print(f"Deleting existing test file: {exists.name}") |
| 50 | + exists.delete_object().execute_query() |
| 51 | + except Exception: |
| 52 | + root.upload("recycle_bin_test.txt", b"Content to be deleted").execute_query() |
| 53 | + test_file.get().execute_query() |
| 54 | + test_file.delete_object().execute_query() |
| 55 | + |
| 56 | + print("Test file deleted to recycle bin.\n") |
| 57 | + print("Recycle bin items can be accessed via the drive's recycleBin.") |
| 58 | + print("See Graph API docs for the exact endpoint:\n") |
| 59 | + print(f" GET /drives/{drive.properties.get('id','?')}/recycleBin/items\n") |
| 60 | + return |
| 61 | + |
| 62 | + if len(items) == 0: |
| 63 | + print("Recycle bin is empty.\n") |
| 64 | + |
| 65 | + # Create a test file, delete it, then show it |
| 66 | + root = drive.root.get().execute_query() |
| 67 | + test_file = root.upload("recycle_bin_test.txt", b"Temporary content.").execute_query() |
| 68 | + print(f"Created test file: {test_file.name}") |
| 69 | + |
| 70 | + test_file.delete_object().execute_query() |
| 71 | + print("Deleted — now in the recycle bin.\n") |
| 72 | + |
| 73 | + # Reload |
| 74 | + items = recycle.items.get().execute_query() |
| 75 | + |
| 76 | + print(f"Recycle bin items ({len(items)}):\n") |
| 77 | + for item in items: |
| 78 | + deleted_dt = item.properties.get("deletedDateTime", item.properties.get("deleted", None)) |
| 79 | + if hasattr(deleted_dt, "strftime"): |
| 80 | + deleted_dt = deleted_dt.strftime("%Y-%m-%d %H:%M") |
| 81 | + original = item.properties.get("name", "?") |
| 82 | + size = item.properties.get("size", "?") |
| 83 | + print(f" {original:35s} size={size:>8} deleted={deleted_dt or '?'}") |
| 84 | + |
| 85 | + # -- Step 3: restore the first item from the bin -- |
| 86 | + if items: |
| 87 | + print("\nRestoring the first item...") |
| 88 | + item = items[0] |
| 89 | + try: |
| 90 | + item.restore().execute_query() |
| 91 | + print(f" ✓ Restored: {item.properties.get('name', '?')}") |
| 92 | + except Exception as e: |
| 93 | + print(f" Restore not available: {e}") |
| 94 | + |
| 95 | + # -- Step 4: permanent delete a specific item (commented out) -- |
| 96 | + # Permanent delete bypasses the bin entirely: |
| 97 | + # item.permanent_delete().execute_query() |
| 98 | + # |
| 99 | + # Or permanently purge from recycle bin: |
| 100 | + # recycle.items["<item-id>"].permanent_delete().execute_query() |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main() |
0 commit comments